如何取消已经运行的ajax请求(在服务器端)

发布于 2024-07-18 08:19:00 字数 126 浏览 6 评论 0原文

我想知道是否有一种简单的方法可以取消 AJAX 请求?

除了在客户端的 XMLHTTPRequest 上调用“中止”之外,还有其他方法可以轻松停止服务器进程吗? 服务器使用 Apache。

谢谢

I was wondering if there's a simple way to cancel an AJAX request?

Beyond just calling an 'abort' on the XMLHTTPRequest on the client side, is there a way to easily stop the server process? The server is using Apache.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

征﹌骨岁月お 2024-07-25 08:19:00

不,绝对没有简单的方法。

我可以想到一些复杂的方法,但它们并不可靠。

您可能会更幸运地开发一个流程来逆转您刚刚运行的请求的更改(我假设这就是您想要停止它的原因。)

No. There's definitely no simple way.

I can think of some complex ways, but they would be unreliable.

You'll probably have more luck developing a process for reversing the changes from a request you've just run (I'm assuming that's the reason you would want to stop it.)

吃颗糖壮壮胆 2024-07-25 08:19:00

发起另一个 Ajax 请求。 我通过试图弄清楚如何防止先前的请求在调用另一个请求时被中断而找到了这个线程。

Initiate another Ajax request. I found this thread by trying to figure out how to prevent a previous request from being interrupted when another is called.

滥情空心 2024-07-25 08:19:00

没有好的方法可以通过网络停止请求本身(正如许多人所说),但停止请求的影响相对容易。 即

在服务器端:您可以通过不断检查cookie或会话等全局变量(充当标志)来退出(或终止脚本所需的任何术语)(再次假设这些会在脚本执行期间发生变化)。 此外,您可以将文件或数据库条目保留为标志(您可以轮询是否存在)。

在客户端:您可以调用 abort (这并不适用于所有浏览器,就像其他人所说的那样),或者您可以在回调的顶部保留一个全局信号量/标志(即检查全局值以查看它是否仍应执行)。 例子:

 window.ajaxIsStillGood = true;

 xhr.onreadystatechange = function( )
 {
     if( 4 === xhr.readyState && 200 === xhr.status )
     {
         if( true === window.ajaxIsStillGood ){ /* still good */ }
     }
 }

 /* bad stuff happens */

 window.ajaxIsStillGood = false; // now the code won't execute

there are no good ways to stop the request itself over the network (as many have said), but it's relatively easy to stop the effects of the request. i.e.

on the server-side: you could exit (or whatever term you'd require to kill the script) by constantly checking global variables (acting as flags) like cookies or sessions (again, assuming these would change during the execution of the script). additionally, you could keep files or database entries as flags (that you can poll for existence).

on the client-side: you could call abort (which doesn't work in all browsers, like others have said), or you could keep a global semaphore / flag at the top of your callback (that checks the global value to see if it should still execute or not). example:

 window.ajaxIsStillGood = true;

 xhr.onreadystatechange = function( )
 {
     if( 4 === xhr.readyState && 200 === xhr.status )
     {
         if( true === window.ajaxIsStillGood ){ /* still good */ }
     }
 }

 /* bad stuff happens */

 window.ajaxIsStillGood = false; // now the code won't execute
掩饰不了的爱 2024-07-25 08:19:00

对于 PHP,我找到了一种使用 session_write_close(); 来停止或中止的方法。 只需将其放在脚本中的 session_start() 之后即可。 如果您使用框架或 OOP,则将其保留为方法的第一行。 发生这种情况是因为会话数据在 ajax 完成之前被锁定。

经测试& 在 xhr.abort() 之后看到另一个页面正在加载

此处找到解决方案提示
使用 jQuery 中止 Ajax 请求

For PHP I have found a way to stop or abort by using session_write_close();. Just put this after session_start() on your script. If you are using framwework or OOP then keep it as first line of your method. This happens because session data is locked until the ajax has finished.

Tested & saw another page is now loading after xhr.abort()

Solution hints found here
Abort Ajax requests using jQuery

jJeQQOZ5 2024-07-25 08:19:00

最简单的方法是发送 503(或类似)HTTP 响应标头并中止服务器端应用程序(或只是中止应用程序)。 假设没有发送数据和/或您检查了客户端的 HTTP 响应标头,您可以将其视为失败的请求。

The easiest way would be just to send a 503 (or similar) HTTP Response header and abort the server-side application (or just abort the application). Assuming no data was sent and/or you checked the HTTP response header client side you can just treat it as a failed request.

治碍 2024-07-25 08:19:00

最简洁的答案是不”。 除非您正在谈论服务器端运行时间非常长的后台进程,在这种情况下,您可以让第二个 Ajax 请求启动其停止。 但是,如果您正在谈论对 Apache/PHP 服务器的常规请求,那么……识别您的请求在哪个 Apache 线程中运行就够麻烦的了。

在开始该过程之前以某种方式验证您的 Ajax 请求会容易得多; 确保它是您想要运行的。 如果您需要用户知道他们正在做的事情不能被中断,您也可以有一个 Javascript 确认过程。

正如史蒂文所言,逆转过程听起来也可能符合您的最佳利益。

The short answer is "no". Unless you're talking about a very long running backgrounded process on the server side, in which case you could have a second Ajax request initiate a stop on it. But if you're talking about a regular request to an Apache/PHP server, then no... identifying which Apache thread your request was running in would be trouble enough.

It would be much easier to verify your Ajax request somehow before even starting the process; make sure it's what you want to be running. You could have a Javascript confirmation process as well if you need the user to be aware that what they are doing cannot be interrupted.

As Steven posted, having a reversal process sounds like it might be in your best interest as well.

心不设防 2024-07-25 08:19:00

如何发送另一个 Ajax 请求来更改由该长进程监控的特定 $_SESSION var?

How about sending another Ajax request that would change particular $_SESSION var which is monitored by that long process?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文