对PHP的XHR请求
您好,我对XHR post请求有问题 在javascript中:对PHP的XHR请求,php,xmlhttprequest,Php,Xmlhttprequest,您好,我对XHR post请求有问题 在javascript中: self.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); self.xhr.setRequestHeader("Method", "POST " + url + " HTTP/1.1"); 在firebug中: Parametersapplication/x-www-form-urlencoded {"u":"andrepade
self.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
self.xhr.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
在firebug中:
Parametersapplication/x-www-form-urlencoded
{"u":"andrepadez","m":"\n...
JSON
m
"sfdsfsdfdsfdsf"
u
"andrepadez"
Source
{"u":"andrepadez","m":"\nsfdsfsdfdsfdsf"}
在.NET中,我将其发布到.ASHX,并在ProcessRequest中执行以下操作:
StreamReader sr = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
javaScriptSerializer.Deserialize<MyClass>(message);
StreamReader sr=newstreamreader(context.Request.InputStream,context.Request.ContentEncoding);
反序列化(消息);
我没有问题
我不知道如何在PHP中获取数据,$\u POST和$\u REQUEST都是空数组
需要帮忙吗?
谢谢您尚未为正在发送的数据设置字段名。PHP要求通过POST输入的数据采用
fieldname=value
格式_POST和其他数组一样是一个数组,PHP数组中存储的每个值都必须有一个键(即表单字段名)
您可以尝试通过从中读取来检索数据php://input:
$post_data = file_get_contents('php://input');
但最简单的解决方案是在XHR调用中提供一个字段名。您尚未为发送的数据设置字段名。PHP要求通过POST输入的数据采用
fieldname=value
格式_POST和其他数组一样是一个数组,PHP数组中存储的每个值都必须有一个键(即表单字段名)
您可以尝试通过从中读取来检索数据php://input:
$post_data = file_get_contents('php://input');
但最简单的解决方案是在XHR调用中提供一个字段名。如果将
内容类型设置为application/x-www-form-urlencoded
,那么PHP自然会希望解析帖子正文。但由于数据实际上并不构成表单数据,因此它将被视为无效而丢弃
您需要设置正确的CT应用程序/json
(实际上并不重要),这样PHP就不用管它了。如果将内容类型
设置为application/x-www-form-urlencoded
,那么PHP自然会希望解析帖子正文。但由于数据实际上并不构成表单数据,因此它将被视为无效而丢弃
您需要设置正确的CT应用程序/json
(实际上并不重要),这样PHP就不用管它了。然后,它可以作为或使用。您正在将内容类型设置为application/x-www-form-urlencoded
,但您正在将内容设置为json。我猜这会让您的PHP Web服务器感到困惑。尝试将您的帖子内容设置为您告诉服务器的内容(application/x-www-form-urlencoded
),或者在php://input
您正在将内容类型设置为应用程序/x-www-form-urlencoded
,但您正在将内容设置为json。我猜这会让您的PHP Web服务器感到困惑。尝试将您的帖子内容设置为您告诉服务器的内容(application/x-www-form-urlencoded
),或者在php://input
非常感谢您。。。我不能使用第一个答案(至少是现在),因为我正在实现一个同事的代码,但第二个答案就像一个Charm,非常感谢你。。。我不能使用第一个答案(至少是现在),因为我正在实现一个同事的代码,但第二个答案很有用