2

我似乎无法让以下工作

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\"
}
4

2 回答 2

0

如果您查看代码,您会回显两个字符串。您需要删除其中一个,很可能是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;
于 2012-12-22T07:56:06.377 回答
0

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

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

看到“qName”已通过。此外,您的 UPDATE 语句目前似乎没有设置任何字段(请参阅:PHP 更新语法)。

于 2012-12-22T08:05:17.733 回答