当以前的尝试失败时,Firefox没有完成一系列的调用 [英] Firefox not completing series of calls when previous attempts failed

查看:111
本文介绍了当以前的尝试失败时,Firefox没有完成一系列的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用一个GET方法来ping一系列的url。我只想平均每一次,不要期待回应。我的脚本适用于Chrome和Safari,但Firefox不能完成以后的请求。

有没有办法触发Firefox每一次进行一系列的调用(精确到五次),而不是在乎它们是否失败?看来,Firefox将不会完成一系列的请求时,第一个失败。

我正在使用JavaScript和jQuery,有一点jQuery.ajax()我已经搜索,无济于事,并已达到我的初学者的技能组的限制。任何洞察力将不胜感激。



(如果您对整个范围感兴趣,可以在基于jquery的独立端口

谢谢。




更新:

经过进一步研究,我相信问题是Firefox不能真正异步地处理这些调用。我有版本的代码使用img调用ping,iframe url调用,并且ajax调用在Chrome和Safari中工作,但是在Firefox中,他们并不像我需要的那样工作。

我们的服务器监控敲响顺序应该看到请求顺序到端口1,2,3,4,5(如使用Chrome或Safari时),但在Firefox,无论我尝试过哪种方法,我都会看到第一次尝试ping端口1两次,然后是端口2,在后续尝试中,我只能看到它是ping端口1.我的状态更新按预期显示,但服务器不按照需要的顺序接听电话。看来,Firefox正在重试失败的调用,而不是每次执行一次,这是我需要做的。

以下是使用简单jquery.ajax调用方法的脚本示例。它在Safari和Chrome中工作,但在Firefox中无法达到预期的效果。当我所有的代码运行,我可以看到状态更新(使用jquery.append函数生成),请求不会发送一次,顺序到我的服务器。

 < script src =http://code.jquery.com/jquery-latest.js>< /脚本> 
< script type =text / javascript>
$(document).ready(function(){
$('button')。click(function(){
$('#knocks')。append('< p> ;敲入...< / p>');
setTimeout(function(){
$ .ajax({url:'https://example.sample.com:1111'});
$('#knocks')。append(< p> 5的完整...< / p>);
},500);
setTimeout function(){
$ .ajax({url:'https://example.sample.com:2222'});
$('#knocks')。append(< p>敲5的完整...< / p>);
},3500);
setTimeout(function(){
$ .ajax({url:'https:/ /example.sample.com:3333'}); $ b $('#knocks')。append(< p> 5的完整...< / p>的敲3); $ b $
setTimeout(function(){
$ .ajax({url:'https://example.sample.com:4444'});
$(' 5)完成...< / p>);
},9500)
setTimeout(func tion(){
$ .ajax({url:'https://example.sample.com:5555'});
$('#knocks')。append(< p> 5个完整的敲5 ...< / p>);
},12000);
setTimeout(function(){
$('#knocks')。append(< p> Knocking is complete ...< br>前往网站:< a href ='http ://example-url.sample-url.com'> http://example-url.sample-url.com< / a>< / p>);
},13000);
});
});
< / script>


解决方案

d可能要继续前进,我虽然会给你一些建议作为一个起点。

为了让你的函数调用真正按顺序执行(或 ),您将必须确保您发出所有后续函数调用案件)一旦诉讼请求完成(成功或失败,在这种情况下,你可能不想进行下一个按序呼叫,并发出完全单独的回应)。



你现在做的方式不被认为是同步,而是实际上是异步,延迟 (或'在后台'超时)。当你希望你的AJAX调用在服务器端同步执行(阻塞)的时候,这可能会导致各种各样的问题。从浏览器重新发出失败或超时请求(出于各种原因,取决于它们的功能集以及它们如何处理失败的请求,缓存等),以在预启用某些预取器时预先发出请求和缓存结果(或然而他们在FF中调用它),然后重新发出它们,如果预取器失败。我相信这与您在Firefox中观察到的类似,可能是这种意外行为的主要罪魁祸首。由于您无法控制最终用户在其浏览器中启用或禁用哪些功能,或者未来版本中实现了哪些新功能,因此您不能期望通过延迟 setTimeout ,即使它们在其他浏览器中似乎是这样做的(可能是因为您的服务器响应速度足以让它们出现)。



在你的代码中,第二个调用只会同时执行(等待第一个完成)达半秒,第三个请求最多需要3秒半, 等等。但即使 setTimeout 阻塞执行(不这样做),它将等待哪个外部请求?第一个,还是第二个?我想你会得到我想说的,以及为什么你的代码不能按预期工作。



相反,你应该通过服务器的响应发出后续的AJAX调用(这实际上就是使用AJAX的要点,否则就不需要了),或者最好创建一个外部监听器函数,根据以前外部调用的状态和/或返回值处理这些调用。如果您还需要处理失败的请求,并继续执行,那么外部侦听器(带有预设的执行堆栈超​​时)是可行的,因为您显然无法依赖失败请求的响应。



你看,浏览器没有问题发出多个并发请求,并用 setTimout 延迟它们并不会阻止预取器尝试并将其响应缓存以备后用。它也不会以阻塞的方式发出请求,下一个等待前一个请求完成,就像你期望的那样。大多数人会乐意利用多达一定数量的并发连接(在客户机上约10个,在服务器上多得多),以加快下载和/或页面呈现过程,并且一些显然已经更高级缓存机制的原因是这个原因,Firefox只是其中之一。



我希望这个清除一些东西,你可以重写你的代码按预期工作。由于我们不知道你的服务器端代码是如何工作的,所以你必须自己写。然而,SE在讨论类似的技术方面存在很多线索,您可能会决定使用这些技术,如果遇到困难,您可以随时提出其他问题,我们将很乐意提供帮助。



干杯!


I have a script that pings a series of urls with a GET method. I only want to ping them each once and do not expect a response. My script works in Chrome and Safari, but Firefox won't complete the later requests.

Is there a way to trigger Firefox to make a series of calls (five, to be precise) once each, and not care if they fail? It seems that Firefox won't complete the series of requests when the first ones fail.

I'm working in javascript and jQuery, with a little jQuery.ajax() thrown in. I've searched, to no avail and have reached the limit of my beginner's skill set. Any insight would be appreciated.

(If you're interested in the full scope, there's code at jquery-based standalone port knocker)

Thank you.


Update:

After further research, I believe the issue is that Firefox isn't handling the calls truly asynchronously. I have versions of code making the pings with img calls, iframe url calls, and ajax calls to work in Chrome and Safari, but in Firefox they're not behaving as I need them to.

Our server monitoring for the knock sequence should see requests come sequentially to ports 1, 2, 3, 4, 5 (as it does when using Chrome or Safari) but in Firefox, no matter which method I've tried, I see the first attempt ping port 1 twice, then port 2, and on subsequent attempts I only see it ping port 1. My status updates appear as expected, but the server isn't receiving the calls in the order it needs them. It seems that Firefox is retrying failed calls rather than executing each one once, in sequence, which is what I need it to do.

Here is a sample of my script using a simple jquery.ajax call method. It works in Safari and Chrome, but doesn't achieve the desired result in Firefox. While all my code runs and I can see the status updates (generated with the jquery.append function), the request aren't sent once each, sequentially to my server.

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
    $('#knocks').append('<p>Knocking...</p>');
    setTimeout(function(){
        $.ajax({url: 'https://example.sample.com:1111'});
        $('#knocks').append("<p>Knock 1 of 5 complete...</p>");
        }, 500);    
    setTimeout(function(){
        $.ajax({url: 'https://example.sample.com:2222'});
        $('#knocks').append("<p>Knock 2 of 5 complete...</p>");
        }, 3500);
    setTimeout(function(){
        $.ajax({url: 'https://example.sample.com:3333'});
        $('#knocks').append("<p>Knock 3 of 5 complete...</p>");
        }, 6500);
    setTimeout(function(){
        $.ajax({url: 'https://example.sample.com:4444'});
        $('#knocks').append("<p>Knock 4 of 5 complete...</p>");
        }, 9500)
    setTimeout(function(){
        $.ajax({url: 'https://example.sample.com:5555'});
        $('#knocks').append("<p>Knock 5 of 5 complete...</p>");
        }, 12000);
    setTimeout(function(){
        $('#knocks').append("<p>Knocking is complete... <br>Proceed to site: <a href='http://example-url.sample-url.com'>http://example-url.sample-url.com</a></p>");
        }, 13000);
});
});
</script>

解决方案

Seeing there's no real answer to your question and you'd probably want to move on, I though I'd give you some suggestions as a starting point.

For your function calls to truly execute in a sequential order (or synchronous, in-order, blocking,...) you will have to make sure you issue all the subsequent function calls (AJAX requests in your case) once the proceeding requests completed (either succeed, or failed, in which case you might not want to proceed with the next in-order call and issue a completely separate response).

The way you're doing it now isn't considered synchronous, instead it is actually asynchronous, delayed (or 'in the background' with a timeout). This might cause all kinds of problems when you expect your AJAX calls to execute synchronously (blocking) at your server end. From browsers re-issuing failed or timed-out requests (for various reasons, depending on their feature set and how they handle failed requests, caching,...) to preemptively issuing requests and caching results when some pre-fetchers are enabled (or however they're calling it in FF), then re-issuing them again if pre-fetcher failed. I believe this is similar to what you observed in Firefox and might be the main culprit for this unexpected behavior. As you can't control what features end user enables or disables in their browser, or what new features they implement in future versions, you can't really expect your server calls to execute asynchronous by delaying their calls with setTimeout, even if they appear to be doing so in other browsers (probably because of your server responding fast enough for them to appear as such).

In your code, the second call would only appear to be executing synchronously (waiting for the first one to complete) for up to half a second, the third request for up to 3 seconds and a half, and so on. But even if setTimeout was blocking execution (which it doesn't), which external request would it be waiting for? The first one, or the second one? I think you get what I'm trying to say and why your code doesn't work as expected.

Instead, you should either issue subsequent AJAX calls through your server's response (which is actually the point of using AJAX, otherwise there's no need for it), or preferably, create an external listener function that will handle these calls according to the status and/or return values of your previous external calls. If you need to handle failed requests as well and continue execution regardless, then the external listener (with preset execution stack timeout) is the way to go, as you obviously wouldn't be able to depend on response of failed requests.

You see, browsers have no problems issuing multiple concurrent requests and delaying them with setTimout doesn't stop pre-fetchers to try and cache their responses for later use either. It also doesn't issue requests in a blocking manner, the next one waiting for the previous one to finish, as you expected them to. Most will be happy to utilize up to a certain number of concurrent connection (~10 on client machines, a lot more on servers) in an effort to speed-up the download and/or page rendering process, and some obviously have even more advanced caching mechanism in place for this very same reason, Firefox being merely one of them.

I hope this clears things up a bit and you'll be able to rewrite your code to work as expected. As we have no knowledge of how your server-side code is supposed to work, you'll have to write it yourself, though. There are however plenty of threads on SE discussing similar techniques that you might decide on using, and you can always ask another question if you get stuck and we'll be glad to help.

Cheers!

这篇关于当以前的尝试失败时,Firefox没有完成一系列的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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