5

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

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

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

4

1 回答 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
  }
}
于 2012-09-02T02:47:30.143 回答