I'm using jQuery 1.7.2 and would like to make a POST request to another domain. It must be a POST request. But this does not work in internet explorer (I tried on IE9); it works on all other browsers.
我正在使用jQuery 1.7.2,并希望向另一个域发出POST请求。它必须是POST请求。但这在Internet Explorer中不起作用(我试过IE9);它适用于所有其他浏览器。
I have this script:
我有这个脚本:
<script>
jQuery.support.cors = true;
jQuery(function() {
$.ajax({
crossDomain : true,
cache: false,
type: 'POST',
url: 'http://someotherdomain/test.php',
data: {},
success: function(da) {
console.log(JSON.stringify(da))
},
error: function(jqxhr) {
console.log('fail')
console.log(JSON.stringify(jqxhr))
},
dataType: 'json'
});
});
</script>
I get back the error:
我收回错误:
{"readyState":0,"status":0,"statusText":"Error: Access denied.\r\n"}
My PHP file looks like this:
我的PHP文件如下所示:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS');
echo json_decode(array('success' => 'yes'));
4 个解决方案
#1
2
Internet Explorer (including IE9) does not support CORS. You have to proxy all your cross-domain request (post to PHP script on the same domain, that reposts with curl your query and returns the response)
Internet Explorer(包括IE9)不支持CORS。您必须代理所有跨域请求(在同一域上发布到PHP脚本,重新发布卷曲查询并返回响应)
#2
2
To support CORS in IE < 10, you must modify the ajax method to use the XDomainRequest object. This plugin does it for you: https://github.com/jaubourg/ajaxHooks
要在IE <10中支持CORS,必须修改ajax方法以使用XDomainRequest对象。这个插件为你做到了:https://github.com/jaubourg/ajaxHooks
#3
1
Your script looks correct but I believe you need to change:
您的脚本看起来正确但我相信您需要更改:
header('Access-Control-Allow-Origin: *');
to
header('Access-Control-Allow-Origin: x-requested-with');
or
header('Access-Control-Allow-Origin: {Origin}');
Where {Origin} is the value of the Origin header. It's my understanding that just putting '*' won't work if an Origin has been given.
其中{Origin}是Origin标头的值。我的理解是,如果给出了Origin,那么放'*'将不起作用。
Also, IE8 and IE9 have limited support for this but it does work if you put jQuery.support.cors = true
, as you've done.
此外,IE8和IE9对此的支持有限,但如果你把jQuery.support.cors = true,它确实有效。
#4
0
This works in IE9.
这适用于IE9。
<!DOCTYPE html>
<head>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
var url = "http://msdn.microsoft.com/en-us/library/windows/desktop/ms759148(v=vs.85).aspx";
function getRequest() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
catch (e) {alert("Error while getting 6.0");}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
catch (e) {alert("Error while getting 3.0");}
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {alert("Error while getting 2.0");}
throw new Error("This browser does not support XMLHttpRequest.");
};
var request = getRequest();
request.open("POST", url, false);
request.send();
alert("Content from :"+url+":"+ request.responseText);
</script>
</head>
<h1>AJAX ActiveX</h1>
#1
2
Internet Explorer (including IE9) does not support CORS. You have to proxy all your cross-domain request (post to PHP script on the same domain, that reposts with curl your query and returns the response)
Internet Explorer(包括IE9)不支持CORS。您必须代理所有跨域请求(在同一域上发布到PHP脚本,重新发布卷曲查询并返回响应)
#2
2
To support CORS in IE < 10, you must modify the ajax method to use the XDomainRequest object. This plugin does it for you: https://github.com/jaubourg/ajaxHooks
要在IE <10中支持CORS,必须修改ajax方法以使用XDomainRequest对象。这个插件为你做到了:https://github.com/jaubourg/ajaxHooks
#3
1
Your script looks correct but I believe you need to change:
您的脚本看起来正确但我相信您需要更改:
header('Access-Control-Allow-Origin: *');
to
header('Access-Control-Allow-Origin: x-requested-with');
or
header('Access-Control-Allow-Origin: {Origin}');
Where {Origin} is the value of the Origin header. It's my understanding that just putting '*' won't work if an Origin has been given.
其中{Origin}是Origin标头的值。我的理解是,如果给出了Origin,那么放'*'将不起作用。
Also, IE8 and IE9 have limited support for this but it does work if you put jQuery.support.cors = true
, as you've done.
此外,IE8和IE9对此的支持有限,但如果你把jQuery.support.cors = true,它确实有效。
#4
0
This works in IE9.
这适用于IE9。
<!DOCTYPE html>
<head>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
var url = "http://msdn.microsoft.com/en-us/library/windows/desktop/ms759148(v=vs.85).aspx";
function getRequest() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
catch (e) {alert("Error while getting 6.0");}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
catch (e) {alert("Error while getting 3.0");}
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {alert("Error while getting 2.0");}
throw new Error("This browser does not support XMLHttpRequest.");
};
var request = getRequest();
request.open("POST", url, false);
request.send();
alert("Content from :"+url+":"+ request.responseText);
</script>
</head>
<h1>AJAX ActiveX</h1>