Question

I'm a bit puzzled by this. I want to get my hands via xhr, on the client side, on the contents of a json file from the server and I can't seem to manage that.

No Jquery, so plain XMLHttprequest.

function getJson(user, value) {
  params = "user=" + user + "&value=" + value;
  request = new ajaxRequest(); //wrapper around xmlhttprequest
  request.open("POST", "checkSJ.php", true);  
  request.setRequestHeader("Content-type", "application/json");
  request.setRequestHeader("Content-length", params.length);
  request.setRequestHeader("Connection", "close");
  request.onload = function() {
  var status = request.status;
  if (this.readyState == 4)
        if (this.status == 200)
            if (this.responseText != null)
                O('contents').innerHTML = this.responseText;
 }
 request.send(params);
}

When I print_r the contents of the $_POST array I get an empty array although when I check out the source in my Firebug the parameters with their values are being sent.

Any idea on why this is not working? I'd be content with any working example that accomplishes this task.

Was it helpful?

Solution

Put

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

Remove

request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Content-type", "application/json");

as you are not sending JSON, also content-length is not needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top