简体   繁体   中英

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"] ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM