0

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'];
3
  • application/x-www-form-urlencoded means 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 the xmlhttp.status === 200 in xmlhttp.onreadystatechange. Commented Jun 23, 2011 at 5:15
  • @Saxoier Thank you I just edited my code according to your instructions but unfortunately it's still not working Commented Jun 23, 2011 at 5:50
  • Your first version has already worked. Maybe you redirect with apache's mod_rewrite (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. Commented Jun 23, 2011 at 12:49

3 Answers 3

1

You're sending JSON data, but the content-type is set to application/x-www-form-urlencoded. You should either send form/encoded data (var obj="action=nothing") or set the content-type to JSON (application/json)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I just changed it to application/json but it's still not working
1

James' solution works just fine, but if you are looking to send data using the application/json content type then you have to access the data in a different way.

For what you have server side,

if(isset($_POST['jsonString'])) 
echo $_POST['jsonString'];

change this (like James did):

xmlhttp.setRequestHeader("Content-type","application/json");

to

xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

IF YOU WOULD LIKE TO USE THE application/json content type then you must change how you access it server side with:

    $json_string = file_get_contents('php://input');
    $json_object = json_decode($json_string); 
    echo $json_object->action;

Comments

0

This is working for me:

<html>
<head>
<script src='json.js'></script>
</head>
<body>

<script>
var obj = {"action": "nothing"};
var jsonString = "jsonString=" + JSON.stringify(obj);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","whereIPutThePHP.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-Length",jsonString.length);

xmlhttp.onreadystatechange = function() 
{           
      if(xmlhttp.readyState === 4 && (xmlhttp.status === 200)){
          alert(xmlhttp.responseText);
      } 
}
xmlhttp.send(jsonString);
</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.