javascript 如何在 IE CORS 中发送 POST 数据(使用 XDomainRequest)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12025169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:06:30  来源:igfitidea点击:

How to Send POST data in IE CORS ( with XDomainRequest)

javascriptjquerycors

提问by xd44

I've been looking for a simple example of how to send POST data in a cross domain request in IE (with the XDomainRequest object).

我一直在寻找一个简单的例子,说明如何在 IE 中的跨域请求中发送 POST 数据(使用 XDomainRequest 对象)。

I've been able to make a simple POST request, but haven't been able to add POST data to it.

我已经能够发出一个简单的 POST 请求,但无法向其中添加 POST 数据。

Any help is appreciated, thanks!

任何帮助表示赞赏,谢谢!

回答by ilyasavitski

Try something like this:

尝试这样的事情:

var xdr;
function err() {
    alert('Error');
}
function timeo() {
    alert('Time off');
}
function loadd() {
    alert('Response: ' +xdr.responseText);
}
function stopdata() {
    xdr.abort();
}   
xdr = new XDomainRequest();
if (xdr) {
    xdr.onerror = err;
    xdr.ontimeout = timeo;
    xdr.onload = loadd;
    xdr.timeout = 10000;
    xdr.open('POST','http://example.com');
    xdr.send('foo=12345');
    //xdr.send('foo=<?php echo $foo; ?>'); to send php variable
} else {
    alert('XDR undefined');
}

Server side (php):

服务器端(php):

header('Access-Control-Allow-Origin: *');

if(isset($HTTP_RAW_POST_DATA)) {
  parse_str($HTTP_RAW_POST_DATA); // here you will get variable $foo
  if($foo == 12345) {
    echo "Cool!"; // This is response body
  }
}