带有json的jQuery和Ajax-在IE中失败 [英] jQuery and Ajax with json - fails in IE

查看:46
本文介绍了带有json的jQuery和Ajax-在IE中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery(1.7.0)对Spotify进行json/ajax调用.以下代码在Chrome和Firefox中可以正常运行,但会在IE中导致错误(错误:访问被拒绝.).

I'm using jQuery (1.7.0) to make a json/ajax call to Spotify. The following code works fine in Chrome and Firefox, but causes an error (Error: Access is denied.) in IE.

 $.ajax({
     url: 'http://ws.spotify.com/lookup/1/.json',    
     type: 'GET',    
     dataType: 'json',    
     cache: true,    
     data: {    
       uri: "someartist",    
       extras: "album"    
     },    
     success: successfn,    
     error:function(xhr, status, errorThrown) {    
       alert("networking error: "+errorThrown+'\n'+status+'\n'+xhr.statusText);    
     }
 });

在Chrome和FF中调用成功函数,但在IE中使用上述消息调用错误函数.我已将cors设置为true:jQuery.support.cors = true;.

The success function is called in Chrome and FF, but the error function is called in IE with the above message. I have set cors to true: jQuery.support.cors = true;.

它既可以在本地的Chrome和FF上运行,也可以在我的服务器上运行,在IE的本地版本上可以运行,但不能在服务器上运行.更改cache: false会在Spotify端引起问题-不添加其他参数,因此出现错误请求"错误.

It works on Chrome and FF both locally and on my server, it works in IE locally but not on the server. Changing cache: false causes problems at the spotify end - doesn't line additional parameters, so I get a "bad request" error.

感谢任何指针.

谢谢

Abo

推荐答案

您依靠Spotify网址在其标头中提供Access-Control-Allow-Origin:*,以允许来自所有域的跨域请求.但是Internet Explorer不支持此功能,因此它会拒绝访问.

You are relying on the spotify url to give a Access-Control-Allow-Origin:* in their header to allow cross domain requests from all domains. Internet explorer however doesn't support this, so it gives access denied.

access-control-allow-origin 进行了解释. (TLDR:服务器可能在其标头中允许跨域Ajax)

access-control-allow-origin explained. (TLDR: Servers may allow cross domain ajax in their headers)

如果您需要在IE中使用此功能,则可以使用Spotify的JSONP API(如果他们有一个)或在Flash中发出AJAX请求,该请求可在所有浏览器中使用,并将请求响应数据传递给您的javascript.

If you need this to work in IE, you could use spotify's JSONP API if they have one or make the AJAX request in flash, which works in all browsers and passes the requests response data to your javascript.

这篇关于带有json的jQuery和Ajax-在IE中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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