跨域jQuery Ajax在IE9中不工作

跨域jQuery Ajax在IE9中不工作

问题描述:

我尝试使用jQuery访问REST Web服务。根据Firebug,该服务正确地将Access-Control-Allow-Origin设置为*,并且使用Chrome / Firefox访问它没有问题。然而,在IE它不工作。

I'm trying to access a REST web service using jQuery. The service has correctly got Access-Control-Allow-Origin set to * according to Firebug, and there are no problems accessing it with Chrome/Firefox. However, in IE it doesn't work.

我已经查看了这个问题的很多变化已经发布,但到目前为止没有解决方案

I've looked through plenty of the variations of this question that have already been posted, but so far none of the solutions have worked for me.

这里是我的代码(简化成功/失败函数内容以方便阅读):

Here is my code (simplified the success/failure function contents for ease of reading):

$.support.cors = true;
$.ajax({
   url: 'https://api.guildwars2.com/v1/maps.json?callback=?',
   cache: false,
   type: 'POST',
   dataType: "jsonp",
   success: function() { alert("Success!"); },
   error: function() { alert('Failed!'); }
});

它也不能用于 GET 作为类型,没有或没有回调。

It also doesn't work with GET as the type, nor with or without the callback.

我也试着让它没有jQuery工作,并没有成功:

I have also tried making it work without jQuery and haven't been successful:

var xdr = new XDomainRequest();
var url = "https://api.guildwars2.com/v1/maps.json?callback=?";
if(window.XDomainRequest)
{
    if(xdr)
    {
        xdr.onsuccess = function(){alert('Success!');};
        xdr.open("get",url);
        xdr.send();
    }
    else
    {
        alert('Failed!');
    }
}

任何人都可以给我的建议,因为我正在考虑在我自己的服务器上使用PHP中的 file_get_contents()简单地镜像Web服务,虽然这将是最后的手段,因为它会吞噬更多的带宽。

Any advice anybody could give me would be much appreciated, as I'm considering simply mirroring the web service using file_get_contents() in PHP on my own server, although that would be a last resort as it would gobble up a lot more bandwidth.

这个插件为我解决了这个问题。

This plugin solved it for me.

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest