不同的浏览器如何通过同步XHR处理超时 [英] How do the different browsers handle timeout with synchronous XHR

查看:156
本文介绍了不同的浏览器如何通过同步XHR处理超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

星期三,我维护的AJAXy CRM系统对许多用户来说都崩溃了,因为Firefox开始报告窗口上下文中的同步模式不支持XMLHttpRequest的timeout属性的使用".其他主要浏览器仍然可以正常工作,因此,我建议人们不要使用Firefox.

On Wednesday the AJAXy CRM system I maintain broke for many users, because Firefox started reporting "Use of XMLHttpRequest's timeout attribute is not supported in the synchronous mode in window context" . The other major browsers still work fine so as a workaround I have advised people not to use Firefox.

据我了解,同步请求是一件坏事,因此我只能假设Firefox的一些最新更新已阻止它容纳坏事.所有先前的讨论(在这里和在网络上)都暗示将timeout属性与同步XMLHttpRequest对象一起使用根本不起作用,这使我想知道为什么它显然起作用(截至周三在Firefox上除外).

From what I understand synchronous requests are A Bad Thing, so I can only assume some recent update to Firefox has stopped it from accommodating Bad Things. All previous discussions (here and on the web) imply the use of the timeout attribute with synchronous XMLHttpRequest objects shouldn't work at all, which leads me to wonder why it apparently does (except on Firefox as of Wednesday).

其他浏览器/旧Firefox是否确实在不应"的地方实施超时行为,还是只是忽略该异常并继续执行(而不是像新版Firefox那样进行救助)?

Are the other browsers / old Firefox actually implementing timeout behaviour where they "shouldn't", or do they just ignore the exception and continue execution (rather than bailing out like new Firefox)?

我现在无法访问代码,但它遵循以下原则: if (c.somekindoftimeouthandler !== "unassigned" && this.timeout) {
c.timeout = this.timeout
//more stuff about the handler etc...
}
其中"this"很复杂,大概是从XMLHTTPRequest继承而来的,"c"是指特定AJAX请求或某些东西的特定对象.我会很干净,我对Javascript和这个特定的基于Sugar的CRM都一无所知,但是代码的本质很简单.这是发出请求的一种通用的面向对象的方法,当请求超时时,将处理大约4行.我觉得这是一个抽象的基础,应该从中抽象出特定的请求(或请求实体). Firefox Javascript引擎在this.timeout上令人窒息.

I can't get to the code right now but it was along the lines of: if (c.somekindoftimeouthandler !== "unassigned" && this.timeout) {
c.timeout = this.timeout
//more stuff about the handler etc...
}
where 'this' is come complicated thing presumably inherited from XMLHTTPRequest and 'c' is a specific object referring to the specific AJAX request, or something. I'll come clean that I know little about both Javascript and this specific Sugar-based CRM, but the essence of the code was simple. It was a generic object-oriented way to make requests, and this particular 4-lines or so dealt with when the request timed out. I get the impression this was an abstract base from which specific requests (or requesting entities) were supposed to specialise. The Firefox Javascript engine chokes on the this.timeout.

我相信我的问题的主旨不过是笼统的-考虑到将超时用于XHR同步的弊端",各种浏览器如何处理?

I believe the thrust of my question is more general though - given the 'badness' of using timeout for synch XHR, how do the various browsers deal with it?

推荐答案

IE允许超时"用于同步XHR.我相信,如果达到了超时时间,它将从send()中引发异常.

IE allows 'timeout' to work for sync XHR. If the timeout time is reached, it'll throw an exception from send(), I believe.

Firefox完全实现了XHR规范所说的:异步XHR支持超时,但同步XHR不支持超时(如果尝试设置,则抛出该超时).

Firefox implemented exactly what the XHR spec says: timeout is supported on async XHR but not supported for sync XHR (throws if you try to set it).

其他浏览器(和旧的Firefox)根本不支持超时;他们甚至没有财产.由于您的脚本正在嗅探该属性是否存在,因此它甚至没有尝试在那些其他浏览器中设置超时,这就是它在那里起作用的原因:您的脚本不是在尝试做坏事,所以那些浏览器不会终止抛出异常.

Other browsers (and old Firefox) don't support timeout at all; they don't even have the property. Since your script is sniffing for whether the property is there, it doesn't even try to set timeout in those other browsers, which is why it works there: your script is not trying to do bad things, so those browsers don't end up throwing an exception.

因此,所有更改都在于Firefox添加了对timeout属性的支持,但是它没有执行IE而是执行规范说明的操作.而且您的脚本假设所有实现该属性的浏览器都将按照IE的方式运行...

So all that changed is that Firefox added support for the timeout property, but instead of doing what IE does it implemented what the spec says to do. And your script was assuming that any browser that implements the property will behavior the way IE does...

这篇关于不同的浏览器如何通过同步XHR处理超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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