I’m having trouble getting the server to recognize a JSON POST request since I changed the code a bit. Previously, a POST request was made for each iteration of a for loop, but now I’ve changed it to include a multi-level JSON array.
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");
}
});
At this point in time, tourList has a length of 6 and results is based on Google Maps marker clicks and produces this in 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}
]
However, nothing shows up as a $_POST variable, as $_POST shows up as type: array[0] in the debugger. So I have no data set to run json_decode upon. From what I can see, the JSON is valid. What is the issue?
Send the string as a post var.
Access the json with
$_POST["json"]I’m not sure what you are returning from your PHP, however, if it is json, it will never be equal to
"success"Edit: One more issue. Your
datatypeparameter should bedataType, and this parameter only defines what dataType is being returned from PHP, not what you are sending it.