1

我正在尝试从我的扩展中使用的网络工作者使用 POST 方法发出异步请求。问题是它对我不起作用。在服务器端,我有 PHP 脚本监听 $_POST 变量中的数据。尽管我能够建立与服务器的连接,甚至可以在 URL (GET) 中传递一些数据,但 $_POST 始终为空。

这是我在网络工作者中使用的最新代码:

var serviceUrl = "http://localhost/pfm/service/index.php";
var invocation = new XMLHttpRequest();
if(invocation){
    invocation.open('POST', serviceUrl, true);
    invocation.setRequestHeader('X-PINGOTHER', 'pingpong');
    invocation.setRequestHeader('Content-Type', 'text/plain');
    invocation.onreadystatechange = function(){processResponse(invocation);};
    invocation.send("action=init");
}

(当我知道问题是同源策略时,从 MDN 网站借来的)

在此之前,我使用了一个相当明显且非常简单的方法:

var serviceUrl = "http://localhost/pfm/service/index.php";
var xhr = new XMLHttpRequest();
xhr.open("POST", serviceUrl, true);
xhr.onreadystatechange = function(){processResponse(receiptStoreRequest);};
xhr.send("action=init");

在这种情况下,请求也通过了,但 $_POST 仍然是空的。

网络工作者是否可能不允许 POST 请求?

现在一切都在 localhost 上进行测试。

4

1 回答 1

0

不要设置内容类型text/plain,但

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

如果您正在为 Firefox 4+ 进行开发,您可能还对FormData 对象感兴趣

于 2010-08-28T02:34:09.147 回答