JDelage
JDelage

Reputation: 13682

Accessing params sent by an Ajax POST request

I am writing an Ajax request that sends a JSON-formatted string in a POST request. Here is the relevant code:

var params=jsonString;
request.onreadystatechange = functionXyz;
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
request.send(params);

My question is how do I access the content of the POST on the other side? In a typical form submission the data is sent as an associative array, but in this case I am not sure how to access the data - what the label is. Is it by calling $_POST["params"]?

Upvotes: 0

Views: 56

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191789

You have to set a label for the json string (which is just a string):

request.send("params=" + encodeURIComponent(params));

Then on the server:

$object = json_decode($_POST['params']);

If you just send a JSON string, you can extract it from the post body, but I think that's unnecessary.

Upvotes: 2

Related Questions