跨子域AJAX工作在Chrome浏览器,IE浏览器不 [英] Cross-subdomain AJAX works in Chrome, not IE

查看:182
本文介绍了跨子域AJAX工作在Chrome浏览器,IE浏览器不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网站local.mydomain.com运行的本地版本。我正在AJAX请求到这是一个AWS服务器上运行,并返回JSON api.mydomain.com。在Chrome中,我可以调用API没有问题。但在IE中,我得到访问被拒绝。

I have a local build of my site running at local.mydomain.com. I'm making ajax requests to api.mydomain.com which is running on an AWS server and returns JSON. In Chrome, I can call the API no problem. But in IE, I get Access Denied.

经过研究,这似乎是一个跨(子)域限制。但我是在IM pression这个限制将适用于这两种浏览器。任何人都可以看到什么可能会错在这里,为什么它可能工作在某些浏览器,而不是别人?

After researching, it seems to be a cross-(sub)domain restriction. But I was under the impression that this restriction would apply to both browsers. Can anybody see what might be going wrong here and why it might work in some browsers and not others?

推荐答案

它看起来像问题是在运输对象,IE8 +要你使用。 jQuery使用任何的ActiveXObject(IE浏览器),或者XMLHtt prequest(所有其他人),但IE 8及以上需要XDomainRequest为Ajax。

It looks like the problem was in the transport object that IE8+ wants you to use. jQuery uses either ActiveXObject (for IE) or XMLHttpRequest (all others), but IE 8 and above requires XDomainRequest for ajax.

你可以做的是通过$ .ajaxSettings.xhr这样,

What you can do is return a custom xhr object via $.ajaxSettings.xhr like this,

// override xhr for browser that use XDR
if ('XDomainRequest' in window && window.XDomainRequest !== null) {

  // override default jQuery transport
  jQuery.ajaxSettings.xhr = function() {
      try { return new XDomainRequest(); }
      catch(e) {
        console.log('test'); 
      }
  };

  // also, override the support check
  jQuery.support.cors = true;
}

我把这个code从这里话题的讨论: <一href="http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/">http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/

I pulled this code from a discussion on the subject here: http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/

绝对看一看,如果你认为你遇到了同样的问题。

Definitely take a look at that if you think you're experiencing the same problem.

这篇关于跨子域AJAX工作在Chrome浏览器,IE浏览器不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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