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

前端之家收集整理的这篇文章主要介绍了javascript – 如何在IE CORS中发送POST数据(使用XDomainRequest)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

最佳答案
尝试这样的事情:

@H_404_14@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):

@H_404_14@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 } }

猜你在找的jQuery相关文章