简体   繁体   English

访问Ajax POST请求发送的params

[英]Accessing params sent by an Ajax POST request

I am writing an Ajax request that sends a JSON-formatted string in a POST request. 我正在编写一个Ajax请求,在POST请求中发送一个JSON格式的字符串。 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? 我的问题是如何访问另一方的POST内容? 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"] ? 是通过调用$_POST["params"]吗?

You have to set a label for the json string (which is just a string): 你必须为json字符串设置一个标签(这只是一个字符串):

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. 如果您只是发送一个JSON字符串,您可以从帖子正文中提取它,但我认为这是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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