简体   繁体   English

有没有一种方法可以执行即发即弃的AJAX请求?

[英]Is there a way to perform fire-and-forget AJAX request?

I'm working on a web app. 我正在使用网络应用程序。 When the program switches channel, it sends a "close" AJAX request to the server with the old channel. 当程序切换频道时,它将使用旧频道向服务器发送“关闭” AJAX请求。 As the server responses, the app just sends an "open" request to the server to open a new channel. 随着服务器的响应,该应用程序仅向服务器发送“打开”请求以打开新频道。 Most of times, this close-wait-done-open-wait-done round runs fast enough, but in case of a special channel, close takes longer than 5 secs, which is unacceptable. 大多数情况下,此关闭等待完成打开等待完成循环的速度足够快,但是在使用特殊频道的情况下,关闭时间超过5秒,这是不可接受的。

I've set a short timeout for these channels' "close" requests, and it returns, but the new "open" request hangs until the server responses the old "close" request, so timeout just pushed the problem away. 我为这些通道的“关闭”请求设置了一个短超时,然后它返回,但是新的“打开”请求一直挂起,直到服务器响应旧的“关闭”请求为止,因此超时将问题解决了。

I've set up a setTimeout() for the long close, so as my program calls the "close", it will be delayed, and the new "open" performs before it, but it's also just tossing the problem, when setTimeout() hits in, there will be a long request "in the background", which eats up the browser's ajax request pool, and with some luck, causes pseudo-ajax-hanging at a random AJAX request. 我已经为长时间关闭设置了setTimeout(),所以当我的程序调用“关闭”时,它将被延迟,并且新的“打开”将在此之前执行,但是当setTimeout( )命中,将在“后台”中有一个长请求,这会吞噬浏览器的ajax请求池,如果运气好的话,会导致在随机AJAX请求中挂起伪ajax。

The app has no interest in the result of the "close" request, so it can be dropped, even if it fails. 该应用程序对“关闭”请求的结果不感兴趣,因此即使失败,也可以将其删除。

What's the best practice to perform such fire-and-forget AJAX requests? 执行此类“一劳永逸”的AJAX请求的最佳实践是什么?

(I'm using Chromium browser on Linux, but the app should run on Firefox, too.) (我在Linux上使用Chromium浏览器,但该应用程序也应在Firefox上运行。)

You can use servlet 3.0 Async feature. 您可以使用Servlet 3.0异步功能。 You can commit the response immediately for your close request and start new thread to do further server processing. 您可以立即为关闭请求提交响应,并启动新线程以进行进一步的服务器处理。 You can refer below link for more information on the same. 您可以参考下面的链接,以获取有关同一内容的更多信息。 http://www.javaworld.com/article/2077995/java-concurrency/asynchronous-processing-support-in-servlet-3-0.html http://www.javaworld.com/article/2077995/java-concurrency/asynchronous-processing-support-in-servlet-3-0.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM