由于我稍微更改了代码,因此我无法让服务器识别 JSON POST 请求。以前,对于 for 循环的每次迭代都会发出一个 POST 请求,但现在我已将其更改为包含一个多级 JSON 数组。
var json=[];
for (var i=0; i < tourList.length; i++){
var data = tourList[i];
json.push({latitude: data.position.ab, longitude: data.position.cb, filename: data.title, stopNum: i});
}
var results= JSON.stringify(json);
console.log(json);
console.log(results);
//this code was previously inside the for loop above, moved it outside
$.ajax({
type: "POST",
url: "../includes/phpscripts.php?action=postTour",
data: results,
datatype: "json",
beforeSend: function(x){
if (x && x.overrideMimeType){
x.overrideMimeType("application/json;charset=UTF-8");
}
},
success: function(data){
if (data == "success")
console.log("Tour update successful");
else
console.log("Tour update failed");
}
});
此时,tourList 的长度为 6,results
基于 Google 地图标记点击并在 Firebug 中生成:
[
{"latitude":43.682211,"longitude":-70.45070499999997,"filename":"../panos/photos/1-prefix_blended_fused.jpg","stopNum":0},
{"latitude":43.6822,"longitude":-70.45076899999998,"filename":"../panos/photos/2-prefix_blended_fused.jpg","stopNum":1},
{"latitude":43.682219,"longitude":-70.450828,"filename":"../panos/photos/3-prefix_blended_fused.jpg","stopNum":2},
{"latitude":43.68218,"longitude":-70.45088699999997,"filename":"../panos/photos/4-prefix_blended_fused.jpg","stopNum":3}
]
但是,没有任何东西显示为$_POST
变量,就像在调试器中$_POST
显示的那样。type: array[0]
所以我没有数据集可以运行json_decode
。据我所知,JSON 是有效的。问题是什么?