【发布时间】:2014-05-31 20:24:25
【问题描述】:
在 Firefox 28.0 中进行简单的 XHR POST 后,我的服务器 php 脚本 POST 数组为空。请求同源。
在我的触发请求的 js 脚本中:
$(function(){
$("#head").click(function(){
var invocation=new XMLHttpRequest();
var url="test.php";
var body="hello";
if (invocation)
{
invocation.open("POST",url, true);
invocation.onreadystatechange=handler;
invocation.send(body);
}
function handler(evtXHR){
if(invocation.readyState===4)
{
if(invocation.status===200)
{
alert("success");
}
}
}
});
});
在我的 php 文件中,我有:
<?php
header('Access-Control-Allow-Origin: *');
$data=$_POST['body'];
var_dump($data);
?>
下面是客户端和服务端的交互:
Request Method: POST
Status Code: HTTP/1.1 200 OK
请求头:
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0
Referer: .......
Pragma: no-cache
Host: ..........
Content-Type: text/plain; charset=UTF-8
Content-Length: 5
Connection: keep-alive
Cache-Control: no-cache
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
请求正文: 你好
响应标头
X-Powered-By: PHP/5.2.17
Transfer-Encoding: chunked
Server: Apache
Keep-Alive: timeout=5, max=100
Date: Wed, 16 Apr 2014 19:04:55 GMT
Content-Type: text/html
Connection: Keep-Alive
access-control-allow-origin: *
请帮助弄清楚发生了什么。为什么请求成功后 POST 数组为空? 谢谢。
【问题讨论】:
-
在我做出 Quentin 建议的更改后,我的 php 脚本中的 var_dump $_POST 仍然为 null,但是,HTTPfox 插件显示内容是 string(5) "hello" whan is use var_dump and "hello " 当我在脚本中使用 echo() 时。我该如何解决这些问题?
标签: javascript http firefox post xmlhttprequest