I am trying to see the json data I sent to the server using XMLHttpRequest but it seems that the server is not receiving it, when I run the javascript the alert window will pop up but doesn't print anything. Anyone know how to solve this problem? Thanks
On the client side, Java script
var obj = {"action": "nothing"};
var jsonString = "jsonString=" + JSON.stringify(obj);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","http://myserver/main.php",true);
xmlhttp.setRequestHeader("Content-type","application/json");
xmlhttp.setRequestHeader("Content-Length",jsonString.length);
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
alert(xmlhttp.responseText);
}
}
xmlhttp.send(jsonString);
On the server,php
if(isset($_POST['jsonString']))
echo $_POST['jsonString'];
application/x-www-form-urlencodedmeans that you have to urlencode the string:encodeURIComponent(JSON.stringify(obj)). You also should send a Content-Length:xmlhttp.setRequestHeader("Content-Length", jsonString.length);. And check thexmlhttp.status === 200inxmlhttp.onreadystatechange.RewriteRule, ...) or mod_alias (Redirect, ...). Then you possibly make an additional GET-Request and loose all your POST-Data. Have a look at Firebug->Network. There shouldn't be a 3xx HTTP status code for this request.