Jquery $ .ajax在跨域调用的IE中失败 [英] Jquery $.ajax fails in IE on cross domain calls

查看:129
本文介绍了Jquery $ .ajax在跨域调用的IE中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 $。ajax 执行跨网域请求。它可以在Firefox和Chrome上运行,但不会在IE 7或8上发出呼叫。任何人都可以告诉我以下是什么问题?


  1. 我已经使用JSON和JSONP(由于一些自定义限制,我停止使用它。)

  2. 我已经使用 Allow-access-control-origin 标题。

  3. 我已经尝试过 https://developer.mozilla.org/en/http_access_control



代码:



  $。ajax({
type:'GET',
url:http://anotherdomain.com / Service / GetControl?id =+ zoneID,
cache:false,
contentType:application / x-www-form-urlencoded,
async:false,
beforeSend :function(request){
// alert('before send');
//request.setRequestHeader(\"X-Requested-With,XMLHttpRequest);
// request .setRequestHeader(X-PINGOTHER,pingpong);
},
success:function(data,status){
// alert(Data returned:+ data);
// alert(Status:+ status);
if(status ==success&& data!=)
$(#+ div。 id).append(data);
else
$(#+ div.id).attr(style,display:none;);
},
错误:function(XMLHttpRequest,textStatus,errorThrown){
alert(textStatus);
alert(errorThrown);
}
});

我已经坚持这个代码为最近2天,并尝试了多个网站,但还没有运气。我希望有人可能有一个工作提示:)

解决方案

您可以检查IE的问题是否依赖于没有定义安全区域允许跨域请求? 请参阅此Microsoftsoft页面了解详情。



OTOH,此页面提到IE7和eariler不能做跨域调用,但IE8可以使用一个不同的对象比XMLHttpRequest,一个JQuery使用。



p>第二个链接是死的,所以我这里写的一些信息,取自返路机


XDomainRequest
支持:IE8



IE团队并没有实现XMLHttpRequest的CORS版本,而是拥有了一个合适的对象,名为XDomainRequest 。 XDomainRequest的使用已经从XMLHttpRequest简化,通过抛出更多的事件(withload可能是最重要的)。



这个实现有一些限制。例如,使用此对象时不会发送Cookie,这可能是服务器端基于Cookie的会话的头痛。此外,ContentType不能设置,这在ASP.NET和可能的其他服务器端语言中提出了一个问题(参见 http://www.actionmonitor.co.uk/NewsItem.aspx?id=5 )。

  var xdr = new XDomainRequest(); 
xdr.onload = function(){alert(READY); };
xdr.open(GET,script.html);
xdr.send();



I am doing a cross domain request using $.ajax. It works on Firefox and Chrome, but does not issue a call on IE 7 or 8. Can anyone tell me what's wrong with the following?

  1. I have used JSON and JSONP (which I stopped using, due to some custom restrictions).
  2. I am already using Allow-access-control-origin header on my site. (Without those, Chrome and Firefox were not making successful requests.)
  3. I have already tried https://developer.mozilla.org/en/http_access_control

Code:

$.ajax({
    type: 'GET',
    url: "http://anotherdomain.com/Service/GetControl?id=" + zoneID,
    cache: false,
    contentType: "application/x-www-form-urlencoded",
    async: false,
    beforeSend: function (request) {
        //alert('before send');
        //request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        //request.setRequestHeader("X-PINGOTHER", "pingpong");
    } ,
    success: function (data, status) {
        //alert("Data returned :" + data);
        //alert("Status :" + status);
        if (status == "success" && data != "")
            $("#" + div.id).append(data);
        else
            $("#" + div.id).attr("style", "display:none;");
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
        alert(errorThrown);
    }
});

I have been stuck on this code for the last 2 days and have tried various tips present on multiple sites, but no luck yet. I hope someone might have a working tip ;)

解决方案

Could you check if the problem with IE relies on not defining security zones to allow cross domain requests? See this microsoft page for an explanation.

OTOH, this page mentions that IE7 and eariler cannot do cross domain calls, but IE8 can, using a different object than XMLHttpRequest, the one JQuery uses. Could you check if XDomainRequest works?

EDIT (2013-08-22)

The second link is dead, so I'm writing here some of its information, taken from the wayback machine:

XDomainRequest Supported: IE8

Rather than implement the CORS version of XMLHttpRequest, the IE team have gone with there own propriety object, named XDomainRequest. The usage of XDomainRequest has been simplified from XMLHttpRequest, by having more events thrown (with onload perhaps being the most important).

This implementation has a few limitations attached to it. For example, cookies are not sent when using this object, which can be a headache for cookie based sessions on the server side. Also, ContentType can not be set, which poses a problem in ASP.NET and possibly other server side languages (see http://www.actionmonitor.co.uk/NewsItem.aspx?id=5).

var xdr = new XDomainRequest();
xdr.onload = function() { alert("READY"); };
xdr.open("GET", "script.html");
xdr.send();

这篇关于Jquery $ .ajax在跨域调用的IE中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆