Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript+;PHP$\u POST数组为空_Php_Javascript_Http Post - Fatal编程技术网

Javascript+;PHP$\u POST数组为空

Javascript+;PHP$\u POST数组为空,php,javascript,http-post,Php,Javascript,Http Post,试图通过xmlhttp.open(“POST”,“url”,true)(javascript)向服务器发送POST请求时,我得到一个空的$\u POST数组 Firebug显示正在发送数据。以下是Firebug的数据字符串:a=1&q=151a45a150…。但是$\u POST['q']不返回任何内容 有趣的是,file\u获取内容('php://input“)确实有我的数据(上面的字符串),但PHP不知何故无法识别它。尝试了$\u POST和$\u REQUEST,但均无效 正在发送的标题:

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

Firebug显示正在发送数据。以下是Firebug的数据字符串:
a=1&q=151a45a150…
。但是
$\u POST['q']
不返回任何内容

有趣的是,
file\u获取内容('php://input“)
确实有我的数据(上面的字符串),但PHP不知何故无法识别它。尝试了$\u POST和$\u REQUEST,但均无效

正在发送的标题:

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: application/x-www-form-urlencoded

标题而不是
text/plain

看起来您缺少正确的内容类型标题。这对于POST请求是必要的:

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

你必须这样写:

xmlhttp.open("POST", "script.php", true);
xmlhttp.send("foo=bar&answer=42");

只是花了几个小时试图找到解决这个问题的方法

我犯了一个愚蠢的错误,将几个我想要作为参数的字符串连接起来,然后在整个过程中调用
encodeURIComponent
。这当然意味着

foo=bar&this=that
变成

foo%3Dbar%26this%3Dthat
这对PHP脚本来说当然是胡言乱语。虽然我怀疑会有很多人会做像这样愚蠢的事情,但我希望这能让人免于我刚才给自己带来的头痛