我無法讓服務器識別JSON POST請求,因爲我稍微更改了代碼。以前,for循環的每次迭代都會發出POST請求,但現在我已將其更改爲包含多級JSON數組。無法將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
的長度是基於谷歌地圖標記的點擊次數和產生此螢火蟲:
[
{"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是有效的。什麼是問題?
謝謝! '成功'是echo聲明的結果,也是我日誌記錄過程的一部分。 – Jason
在這種情況下,請刪除'datatype'參數,因爲您沒有返回JSON。 –