在 Event: navigator.onLine 之后重新启动基于 jquery xhr 对象的 Ajax 请求
我们绑定全局 ajax 处理程序来检查浏览器是否离线:
$(document).ajaxSend(function(event, xhr, settings, response){
if(!navigator.onLine){
xhr.abort();
}
}
然后我们向用户显示一个对话框,表明浏览器已离线,并绑定“在线”事件,以便在浏览器再次上线时隐藏该对话框。
是否有无论如何(即使是一个 hacky)根据旧的适合旧上下文的旧 Ajax 请求重新启动 Ajax 请求?
We are binding global ajax handlers to check if the browser went offline :
$(document).ajaxSend(function(event, xhr, settings, response){
if(!navigator.onLine){
xhr.abort();
}
}
then we are showing a dialog to the user that the browser went offline and bind for the 'online' event to hide the dialog when the browser turns online again.
Is there Anyway (even a hacky one) to restart the Ajax request based on the old which fits in the old context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我能想到的最简洁的方法:
ajaxSend() 处理程序中的条件,用于推送调用放在队列中以供稍后使用或执行整个队列。
Here's the cleanest approach I can think of:
A conditional in the
ajaxSend()
handler that either pushes the calls on the queue for later or executes the entire queue.好吧,你可以使用 jQuery 克隆对象,然后在浏览器重新上线时重新启动你的调用
,不知道这是否真的有效,你可以尝试一下
编辑 - 好吧,我尝试过,但没办法,你不能调用 send()在那个物体上。这是因为
xhr
不是原始请求,而是 jQuery 创建的“假”对象另一种方法可能是这样的:保存设置对象,然后使用以下命令启动另一个 $.ajax 调用
那些设置。
基本上你做
var settingsSaved;
请务必小心,这需要精确的流量控制,因为每次调用 $.ajax 时都会触发另一个
ajaxSend
事件...也许您可以简单地使用以下命令开始一个新的XMLHTTPRequest
来自settingsSaved
对象的值。看看这个小提琴,第一次单击按钮时,调用就会中止。第二次调用以旧设置开始,从那时起所有请求都正常
http://jsfiddle.net/hFmWX/
Well you might clone the object using jQuery and then restart your call when the browser goes back online
don't know if this really works, you could try it
EDIT - Ok i tried it and no way, you can't call send() on that object. This is because
xhr
is not the original request but a 'fake' object created by jQueryA different approach might be this one: You save the settings object and then you start another $.ajax call with
those settings.
Basically you do
var settingsSaved;
Be really careful that this requre an accurate flow control because every time you call $.ajax you trigger another
ajaxSend
event...maybe you could simply start off a newXMLHTTPRequest
using the values from thesettingsSaved
object.Look at this fiddle, the first time you click a button, the call is aborted. The second time the call starts with the old settings and from then on all requests are normal
http://jsfiddle.net/hFmWX/