尝试通过 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/