getJSON 或 AJAX 请求不适用于 IE9

发布于 2024-11-17 18:05:41 字数 740 浏览 2 评论 0原文

我已经尝试解决这个问题几个小时了(也在这里搜索,但没有一个解决方案有效)所以我别无选择,只能希望有人告诉我为什么会发生这种情况以及如何解决它。

这是一个简单的代码,适用于 Firefox,但不适用于 IE9(没有其他版本)

示例代码如下:

http://jsfiddle.net /z5b2J/

来源是这个:

$.ajax({
  url: "http://query.yahooapis.com/v1/public/yql?q=select%20script%20from%20html%20where%20url%3D%27https%3A%2F%2Ftesting.website.com%2F%3Fcid%3D48hgfd45430DD%26id%3D4830F8CF0454312%27&format=json&diagnostics=true&_maxage=86400",
  success: function(){
   alert('hi');
  }
});

出于测试目的,该网站不需要是真实的。

正如您在 Firefox 下的小提琴中看到的那样,会出现一个警告框,上面写着“hi”,但是如果您在 IE9 中运行完全相同的代码,则不会出现警告框。

getJSON 方法也会发生同样的情况,这对我来说是一个问题,因为我想运行一些代码而不是警报,但它不会在 IE9 中运行。

I have been trying to solve this problem for hours (searched here as well but none of the solutions worked) so I had no other option but to hope for someone to tell me why this is happening and how can I fix it.

This is a simple code that works with Firefox but not with IE9 (don't have other versions)

Example code is here:

http://jsfiddle.net/z5b2J/

Source is this one:

$.ajax({
  url: "http://query.yahooapis.com/v1/public/yql?q=select%20script%20from%20html%20where%20url%3D%27https%3A%2F%2Ftesting.website.com%2F%3Fcid%3D48hgfd45430DD%26id%3D4830F8CF0454312%27&format=json&diagnostics=true&_maxage=86400",
  success: function(){
   alert('hi');
  }
});

The website doesn't need to be real for testing purpose.

As you can see in the fiddle under Firefox, an alertbox appears saying "hi" BUT if you run the exact same code in IE9, the alertbox doesn't appear.

The same situation occurs with getJSON method, this is a problem for me because I want to run some code instead the alert but it won't run in IE9.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

坦然微笑 2024-11-24 18:05:41

您是否尝试使用 getJSON() 而不是 ajax ?这是一个跨域请求,并且您正在获取 json,因此这可能是问题所在。

现在它可以在两种浏览器中运行:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20script%20from%20html%20where%20url%3D%27https%3A%2F%2Ftesting.website.com%2F%3Fcid%3D48hgfd45430DD%26id%3D4830F8CF0454312%27&format=json&diagnostics=true&_maxage=86400&callback=?",function(){
   alert('hi');
});

Did you try using getJSON() instead of ajax? This is a cross-domain request and you're fetching json so that it probably the problem.

It's working in both browsers now:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20script%20from%20html%20where%20url%3D%27https%3A%2F%2Ftesting.website.com%2F%3Fcid%3D48hgfd45430DD%26id%3D4830F8CF0454312%27&format=json&diagnostics=true&_maxage=86400&callback=?",function(){
   alert('hi');
});
剪不断理还乱 2024-11-24 18:05:41

IE9的问题取决于先进的缓存管理。

如果清空IE的缓存并重新运行ajax请求:第一次就可以了。

要解决此问题,您必须发送包含“noStore = true”和“Duration = 0”或同等内容的 HTTP 响应。

这是 MVC 中的示例

The problem of IE9 depends on advanced cache management.

If you empty the cache of IE and re-run the ajax request: the first time will work.

To resolve this problem you must send your HTTP response with "noStore = true" and "Duration = 0" or equivalent.

Here is an example in MVC.

醉南桥 2024-11-24 18:05:41

使用 $.getJSON$.ajax 您还必须指定 dataType 参数 'jsonp'

这里是使用 getJSON 的示例:

var webpage = ".... your very long url ....";
var anchor = document.createElement('a');
anchor.href = webpage;
// handle the multiple parameters
anchor.search += ((anchor.search.length > 0) ? "&" : "?");
anchor.search += "callback=?";

$.getJSON(anchor.href, 'jsonp',  function(data, textStatus, jqXHR){
   alert('hi');
});

Using $.getJSON or $.ajax you must also specify dataType parameter 'jsonp'

Here the example using getJSON:

var webpage = ".... your very long url ....";
var anchor = document.createElement('a');
anchor.href = webpage;
// handle the multiple parameters
anchor.search += ((anchor.search.length > 0) ? "&" : "?");
anchor.search += "callback=?";

$.getJSON(anchor.href, 'jsonp',  function(data, textStatus, jqXHR){
   alert('hi');
});
就像说晚安 2024-11-24 18:05:41

我通过在 url 中添加“Duration=0”解决了问题

I solved problem by adding "Duration=0" with the url

带刺的爱情 2024-11-24 18:05:41

如果添加回调=?到 URL,它将被转换为类似callback=jQuery1820719005049791166_1366033695001 的内容。它是函数的名称,在服务器端,您应该将 json 编码的对象包装在此函数调用中。因此,您的响应正文不应该只是 {ok: true},而是 jQuery1820719005049791166_1366033695001({ok: true}); 。这对我有用!

If you add callback=? to the URL, it will be transformed to something like callback=jQuery1820719005049791166_1366033695001 . It is the name of the function and on the server side you should wrap your json encoded object in this function call. So your response body should be not just {ok: true}, but jQuery1820719005049791166_1366033695001({ok: true}); . It worked for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文