本文介绍了无法通过AJAX将JSON作为字符串发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图将JSON作为字符串发送。
然后我有一个PHP后端,它检索这个JSON字符串并使用 json_decode 解析它。

So I'm trying to send a JSON as a string.Then I have a PHP back-end that retrieves this JSON string and parses it using json_decode.

不幸的是,我无法将此JSON作为字符串发送。

Unfortunately, I can't get to send this JSON as a string.

这是我使用的jQuery Ajax脚本:

Here's the jQuery Ajax script I used:

var jsonString = JSON.stringify(checkables);
console.log(jsonString);

$.ajax({
    url: $url,
    type: 'POST',
    data: {ajaxidate: JSON.stringify(jsonString)},
    contentType: "application/json; charset=UTF-8",
    success: function (data)
    {
        // just successful callback
    },
    error: function ()
    {
        // just error callback
    }
});

变量 checkables 包含原始形式作为JSON数据:

Variable checkables contains raw form as JSON data:

应用 JSON.stringify()后,这是现在的样子:
[{name:name,type:multialphanumslug,value:AD},{name:服务器 类型: 主机, 值: 10.1.1.1},{ 名称: 端口, 类型: 号码, 值: 8080},{ name:authid,type:username,value:barryallen}]

After applying JSON.stringify(), this is now how it looks:[{"name":"name","type":"multialphanumslug","value":"AD"},{"name":"server","type":"host","value":"10.1.1.1"},{"name":"port","type":"number","value":"8080"},{"name":"authid","type":"username","value":"barryallen"}]

在后面 - 结束,我有这个PHP脚本:

At the back-end, I have this PHP script:

<?php
    var_dump($_POST);
    die();
?>

现在我认为后端的$ _​​POST现在应该包含这个:

Now I suppose $_POST at back-end should now contain this:

数组(
'ajaxidate'=>[{\name \:\name \,\type \:\multialphanumslug \ \ value\:\ AD\},{\ name\:\ server\,\ type\:\ 宿主\,\ value\:\ 10.1.1.1\},{\ name\:\ port\,\ type\:\ number\,\ value\:\ 8080\},{\ name\:\ authid\,\ type\:\ username \,\value \:\barryallen \}]
);

array( 'ajaxidate' => "[{\"name\":\"name\",\"type\":\"multialphanumslug\",\"value\":\"AD\"},{\"name\":\"server\",\"type\":\"host\",\"value\":\"10.1.1.1\"},{\"name\":\"port\",\"type\":\"number\",\"value\":\"8080\"},{\"name\":\"authid\",\"type\":\"username\",\"value\":\"barryallen\"}]");

但它没有什么都收到以下是捕获的请求:

But it didn't receive anything. Here's the captured request:

来自后端?

我试过POSTMan我收到了预期的正确输出:

I tried with POSTMan and I received an expected correct output:

现在这太荒谬了。

我被困在这两天,试图弄清楚发生了什么或者我错过了什么。任何帮助将不胜感激。

I'm stuck at this for 2 days trying to figure out what's going on or what did I miss. Any help will be greatly appreciated.

推荐答案

您需要解析服务器上的数据:

You need to parse the data on the server:

$myArray = json_decode($_POST['ajaxidate']);
var_dump($myArray);

考虑一下:

<?php
    $a = '[{"a": 1}]';
    $b = json_decode($a);
    var_dump($a);
    var_dump($b);
?>

输出:

string(10) "[{"a": 1}]"
array(1) {
  [0]=>
  object(stdClass)#1 (1) {
    ["a"]=>
    int(1)
  }
}

dataType:'json',tldr:使用它!

dataType: 'json', tldr: Use It!

设置dataType = json时告诉jQuery来自服务器的响应应解释为JSON,因此它将为您解析它并将解析的对象/数组作为成功回调的第一个参数:

When setting dataType = json you tell jQuery that the response from the server should be interpreted as JSON and it will therefore parse it for you and give the parsed object / array as first argument to the success callback:

$.ajax({
   // ...
   dataType: 'json',
   success: function(myJson) {
     console.log(myJson); // this will be a JSON object/array...
   }
});

这篇关于无法通过AJAX将JSON作为字符串发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 23:29
查看更多