jQuery跨域Ajax JSONP调用失败随机为未知的原因在一些IE版本 [英] jQuery Cross-Domain Ajax JSONP Calls Failing Randomly For Unknown Reasons In Some IE Versions

查看:344
本文介绍了jQuery跨域Ajax JSONP调用失败随机为未知的原因在一些IE版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用最新版本的jQuery 1.7.1进行跨域Ajax调用。



我已经广泛测试过了:
Safari,Chrome,Opera,Firefox,IE6 IE7 IE8 IE9(在兼容模式下, )在Win XP和Win 7的所有他们工作完美无瑕。不要在多个测试上失败。所有人都使用标准的默认设置启用JavaScript和Cookie。



在生产中,调用非常罕见失败,但我有几百个用户有。可能是50或100之一。我不能理解这一点,因为在所有情况下:




  • 用户没有使用jQuery的非跨域ajax调用的问题。 (即非跨网域调用工作,因此必须启用JavaScript并支持ajax)。


  • 用户只遇到跨网域ajax调用使用jQuery。即使多次尝试。也就是说用户会有问题或不是,它不会失败,然后工作或工作,然后失败。只有一个或另一个。


  • 以下是受影响的用户的用户代理字符串示例:



    Mozilla / 5.0(兼容; MSIE 9.0; Windows NT 6.1; Trident / 5.0; FunWebProducts)



    Mozilla / 4.0(兼容; MSIE 8.0; 5.1; Trident / 4.0; GTB7.2; .NET CLR 2.0.50727; .NET CLR 3.5.30729)



    Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.1; WOW64; Trident / 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CPNTDF; ibrytetoolbar_playbryte; .NET4.0C) / li>


它似乎影响IE,我从来没有遇到其他浏览器的问题。




  • 如果他们使用不同的浏览器,我告诉他们安装Chrome,问题就消失了。





我只能排除这个问题必须是只能在IE上找到的设置。有人有这个问题吗?有没有人知道哪个IE设置可能阻止跨JSON域响应的跨域Ajax调用。



它不能是通常的AJAX / JSONP /跨域问题,因为这些问题不会发生,当浏览器设置为默认,也因为我已经做了正确的方式:
*结束时停止超级缓存的时间戳字符串
* JSONP响应
etc etc



我可以确认这个电话没有被触发。

解决方案

这个问题一直困扰我最近。我的研究表明jQuery ajax调用由IE缓存,因此IE决定它不需要获取数据,这将解释为什么没有发生。这些SO问题有助于停止jquery加载响应缓存 getjson在IE8中返回缓存数据



看起来像额外的显式ajax设置禁用缓存帮助IE获取消息。所有在jQuery中的ajax调用都应该避免缓存,当json是数据类型,但我不相信它的工作原理。这是看起来帮助我,在你的getJSON()或ajax()调用之前插入的代码。



$。ajaxSetup {cache:false});


I am making cross-domain Ajax calls using the latest version of jQuery 1.7.1. with a JSONP response.

I have already tested it extensively on: Safari, Chrome, Opera, Firefox, IE6 IE7 IE8 IE9 (in compatibility mode and not) on Win XP and Win 7 all of them working flawlessly. Never failing on multiple tests. All of them using the standard default settings with JavaScript and Cookies enabled.

In production, the call very very rarely fails, but i've had several users out of hundreds that have. Probably one out of 50 or 100. I cannot make any sense of this because in all cases:

  • The user did not have problems with non-cross-domain ajax calls using jQuery. (i.e. non-cross-domain calls worked so they must have JavaScript enabled and can support ajax).

  • The user only had problems with cross-domain ajax calls using jQuery. Even after repeated attempts. I.e. the user would either have the problem or not, it would not fail then work or work then fail. Only one or the other.

  • Here are a sample of the user agent strings of the users that where affected:

    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; FunWebProducts)

    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.2; .NET CLR 2.0.50727; .NET CLR 3.5.30729)

    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CPNTDF; ibrytetoolbar_playbryte; .NET4.0C)

It only seems to affect IE, i've never had the problem with other browsers.

  • The problem goes away if they use a different browser, i told them to install Chrome.

  • The problem cannot be connection, computer or OS based as changing browsers worked.

I can only rule out that it must be a setting that can only be found on IE. Has anyone had this issue? Does anyone have an idea of which IE setting might be blocking cross-domain Ajax calls with a JSONP response.

It cannot be the usual AJAX/JSONP/Cross-domain problems, since these problems do not occur when the browsers are set to default, and also because i've already done it the proper way: * timestamp string on the end to stop super caching * JSONP response etc etc

I can confirm that the call is not firing at all.

解决方案

This issue has been plaguing me lately too. My research indicates that jQuery ajax calls are cached by IE so IE decides it doesn't need to get the data which would explain why nothing is happening. These SO questions helped Stop jquery load response from being cached and getjson returning cached data in IE8

It seems like the extra explicit ajax setting to disable caching helped IE get the message. All of the ajax calls in jQuery are supposed to avoid caching when json is the data type but I'm not convinced that it works. This is the code that seemed to help me, which you insert right before your getJSON() or ajax() call.

$.ajaxSetup({ cache: false });

这篇关于jQuery跨域Ajax JSONP调用失败随机为未知的原因在一些IE版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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