【问题标题】:Can't process json sent by jquery ajax无法处理jquery ajax发送的json
【发布时间】:2012-12-22 07:51:22
【问题描述】:

我似乎无法让以下工作

restoJSON = { "name" : "Bloesem", "qName" : "bloesem", "address" : "Binnen Dommersstraat 13", "area" : "Jordaan", "tel" : "770 0407", "cuisine" : "European", "comment" : "Ver``rassingsmenu - slow service but 'gezellig' atmosphere", "booking" : "", "website" : "http://www.restaurantbloesem.nl/", "link" : "/?p=6", "rating" : 3, "price" : "3", "lat" : "52.382917", "lng" : "4.8854370000000245", "heading" : "0", "pitch" : "0", "zoom" : "0" };

     jQuery.ajax({
       type: "POST",
       url: "updateDatabase.php",
       data: restoJSON,
       dataType: "text",
       success: function(response, stat)
       {
         console.log("Response: " + response);
       },
       error: function()
        {
           console.log(arguments);
        }       
     });

然后 updateDatatbase.php 在这些行上有变体用于调试

echo "name: " . $_POST['qName'];
$json = json_decode($_POST['data'], true);
echo " " . $json['qName'];
foreach($_POST as $key=>$val) {
echo $key . "-x-" . $val;
}

我是一个相对的菜鸟,通过复制这个论坛的建议来完成工作,而不是完全理解。控制台日志如下 - 注意它是如何拆分链接行中的 =

Response: name: 
{"name":"Bloesem",
"qName":"bloesem",
"address":"Binnen_Dommersstraat_13",
"area":"Jordaan",
"tel":"770_0407",
"cuisine":"European",
"comment":"Verrassingsmenu_-_slow_service_but_'gezellig'_atmosphere",
"booking":"",
"website":"http://www_restaurantbloesem_nl/",
"link":"/?p-x-6\",
\"rating\" : 3,
\"price\" : \"3\",
\"lat\" : \"52.382917\",
\"lng\" : \"4.8854370000000245\",
\"heading\" : \"0\",
\"pitch\" : \"0\",
\"zoom\" : \"0\"
}

【问题讨论】:

  • 您可能只想使用 $.post() 来简化流程

标签: php mysql jquery


【解决方案1】:

当通过 jQuery.ajax 发送数据时,POST 变量被设置为 restoJSON 中的键/值。因此,在 PHP 中不需要 JSON 解码。只需这样做:

echo "name: " . $_POST['qName'];

查看“qName”是否已通过。此外,您的 UPDATE 语句目前似乎没有设置任何字段(请参阅:PHP Update Syntax)。

【讨论】:

  • 您认为我传递句子和特殊字符会导致问题吗?
  • 这应该不是问题,只要字符被转义(如果需要)。您是否也在查看 POST 请求,以确保正确发送数据?看起来 restoJSON 作为字符串传递给 jQuery.ajax,而不是基于您发布的输出的对象。这就是为什么将“=”替换为“-x-”的原因。
  • 好的,所以我放弃了 JSON 和 : 分隔符和围绕内容的 ""。现在我正在使用 = 和 escape() 并且一切正常。我觉得我错过了什么,但最终结果有效
【解决方案2】:

如果您查看代码,您会回显两个字符串。您需要删除其中一个,很可能是echo $query;

所以尝试改变:

echo "name: " . $json['qName'];
echo $query;

只是:

echo "name: " . $json['qName'];

您还缺少restoJSON 末尾的分号,并且它没有使用var 声明为变量。

改变这个:

restoJSON = { "name" : "Bloesem", "qName" : "bloesem", "address" : "Binnen Dommersstraat 13", "area" : "Jordaan", "tel" : "770 0407", "cuisine" : "European", "comment" : "Ver``rassingsmenu - slow service but 'gezellig' atmosphere", "booking" : "", "website" : "http://www.restaurantbloesem.nl/", "link" : "/?p=6", "rating" : 3, "price" : "3", "lat" : "52.382917", "lng" : "4.8854370000000245", "heading" : "0", "pitch" : "0", "zoom" : "0" }

收件人:

var restoJSON = { "name" : "Bloesem", "qName" : "bloesem", "address" : "Binnen Dommersstraat 13", "area" : "Jordaan", "tel" : "770 0407", "cuisine" : "European", "comment" : "Ver``rassingsmenu - slow service but 'gezellig' atmosphere", "booking" : "", "website" : "http://www.restaurantbloesem.nl/", "link" : "/?p=6", "rating" : 3, "price" : "3", "lat" : "52.382917", "lng" : "4.8854370000000245", "heading" : "0", "pitch" : "0", "zoom" : "0" };

最后,您将数据作为key/value 对发送,因此没有理由json_decode() 他们。

改变这个:

$json = json_decode($_POST['data'], true);

收件人:

$json = $_POST;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多