php - Javascript + PHP $_POST 数组为空

标签 php javascript http-post

尝试通过 xmlhttp.open("POST", "url", true) (javascript) 向服务器发送 POST 请求时,我得到一个空的 $_POST 数组.

Firebug 显示正在发送数据。以下是来自 Firebug 的数据字符串:a=1&q=151a45a150...。但是 $_POST['q'] 什么也没返回。

有趣的是,file_get_contents('php://input') 确实有我的数据(上面的字符串),但 PHP 不知何故无法识别它。尝试了 $_POST 和 $_REQUEST,没有任何效果。

正在发送的 header :

POST /test.php HTTP/1.1
Host: website.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401        Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://website.com/
Content-Length: 156
Content-Type: text/plain; charset=UTF-8
Pragma: no-cache
Cache-Control: no-cache

感谢您的任何建议。

最佳答案

您似乎缺少正确的 Content-Type header 。这对于 POST 请求是必需的:

xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

关于php - Javascript + PHP $_POST 数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2673282/

相关文章:

php - laravel,查询构建器,(或)查询未按预期工作的地方

javascript - 使用 JavaScript 将 margin-top 设置为页面的高度

Android NameValuePair 不接受字符串

javascript-如何使用 html 表单进行面积计算

php - 无法在 WAMP 上运行 PHP 脚本

php - 如果开始时间和结束时间相同,时间选择器返回空值

php - 无法通过 PHP 将 NSData 上传到 MySQL

javascript - Vue js,检测值何时更改以突出显示其文本

JavaScript 正则表达式三部分

iphone - NSURLRequest POST 到谷歌应用引擎?