jsonp comet 挂起请求导致丑陋的“加载”浏览器上的状态
我正在使用 jsonp 进行跨域 comet 请求,“正在加载”状态确实很烦人。
有什么方法可以用javascript抑制这种情况吗?
对于那些不熟悉 jsonp 的人来说,它基本上会注入一个脚本标记,但在我的情况下,我将请求挂在我的服务器上,直到稍后才返回请求。
在此期间,浏览器将我的请求视为“正在加载”状态。
我正在使用这个: http://code.google.com/p/jquery-jsonp/ !
提前致谢
I'm using jsonp to do cross-domain comet requests, and the "loading" status is really annoying.
Is there any way to suppress this with javascript?
For those who are unfamiliar with jsonp, it basically injects a script tag, except in my case, I'm hanging the request on my server without returning the request until a later time.
During this time, browsers see my request as a "loading" state.
I am using this: http://code.google.com/p/jquery-jsonp/
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在页面加载完成后开始第一个请求,您应该能够避免加载指示器。
If you begin your first request after the page has finished loading, you should be able to avoid the loading indicator.
据我所知,无论你为什么拥有它,都没有办法使用 Javascript 来抑制加载状态。
然而,跨域 COMET 至少有一个替代方案,它不会首先触发加载状态。 XMLHttpRequest 不会设置加载状态,根据我的测试,允许跨域 XHR 的 CORS(跨源资源共享)规范得到了很好的支持。
基本上,支持如下:(根据浏览器文档和我自己对我正在从事的项目的测试)
完全支持:
未经测试,但应该完全支持:
有限支持:
特别不受支持:
这不是完整的列表,但它是每个带有用户脚本的浏览器我可以找到测试的支持。我已经花时间在CORS 维基百科页面上引用了我的资料来源,如果您想要他们。
我能想到的最简单的解决方案是 测试 CORS 然后回退到 JSONP,以便使用现代浏览器的人获得完美的体验,而使用旧浏览器的人将加载状态视为不可避免的副作用。
以下是关于CORS 工作原理的 MDC 页面。
As far as I know, there is no way to suppress the loading status using Javascript, regardless of why you have it.
However, there is at least one alternative for cross-domain COMET which wouldn't trigger the loading status in the first place. XMLHttpRequest doesn't set the loading status and, according to my tests, the CORS (Cross-Origin Resource Sharing) spec which allows cross-domain XHR is pretty well supported.
Basically, support is as follows: (According to a mix of browser documentation and my own tests for a project I'm working on)
Full support in:
Untested, but should be fully supported:
Limited support in:
Notably unsupported:
It's not a complete list, but it's every browser with userscript support I could find to test. I've already taken the time to cite my sources on the CORS wikipedia page if you want them.
The simplest solution I can think of would be to test for CORS and then fall back to JSONP so that people using a modern browser get a perfect experience and people using something older see the loading status as an unavoidable side-effect.
Here's the MDC page on how CORS works.