5

Is there anything special I have to do to a JSON object before I send it with AJAX? My code looks like this:

runAjax(JSON.stringify(data));

}

function runAjax(JSONstring)
{
    ajax = getHTTPObject();
    var params = "?data=" + JSONstring;
    ajax.open("POST", "createtrip.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", params.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.onreadystatechange = serverSpeaks;
    ajax.send(params);
}

Right now the server is not receiving the data. I get null on the server side but the client side JSONString is set. Is there something I'm doing wrong?

1
  • you can check with Firebug if you have an error or call to server Commented Aug 27, 2009 at 17:12

3 Answers 3

6

You are sending data over POST, you don't need the '?' character at the beginning of the params variable, also I recommend you to encode the JSONString to avoid problems.

Note that you are missing the var statement for the ajax variable, this is declaring it globally (window.ajax) and I think that you don't need it globally...

function runAjax(JSONstring) {
  var params = "data=" + encodeURIComponent(JSONstring), 
      ajax = getHTTPObject();

  ajax.open("POST", "createtrip.php", true);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.setRequestHeader("Content-length", params.length);
  ajax.setRequestHeader("Connection", "close");
  ajax.onreadystatechange = serverSpeaks;
  ajax.send(params);
}
Sign up to request clarification or add additional context in comments.

3 Comments

my var statement is at the beginning of the file as I use if for other things.
I know this is about THREE YEARS after the fact... but in JavaScript scope is with respect to the function. So CMS's usage of var there is completely appropriate. Just wanted to point that out for anyone who might wonder why it was there. Scope in JS is a bit different than other languages we may be used to.
It would be better if you remove these two lines : ajax.setRequestHeader("Content-length", params.length); ajax.setRequestHeader("Connection", "close");
0

You should pass through an encoder to send the data correctly. Of course you would first have to see that the variable "data" is well formed as "JSON".

lib to encode/decode

other link encode/decode

Comments

0

The server can deal with post body like name1=value&name2=value2.

If you are using PHP, you can receive the json string by:

$data = file_get_contents("php://input");

2 Comments

But can it deal with "?data=value"?
Did you try to monitor the HTTP traffic using firebug or wireshark? They can give you more information about this Ajax request.

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.