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

5

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

我已经成功地发出了一个简单的POST请求,但是无法向其中添加POST数据。

任何帮助都将不胜感激,谢谢!

1个回答

10

试试这样做:

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');
}

服务器端(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
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接