How to cancel ajax request that has run (on server

2019-02-21 13:18发布

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

7条回答
男人必须洒脱
2楼-- · 2019-02-21 13:48

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.

查看更多
看我几分像从前
3楼-- · 2019-02-21 13:55

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

查看更多
够拽才男人
4楼-- · 2019-02-21 13:56

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.)

查看更多
forever°为你锁心
5楼-- · 2019-02-21 13:58

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.

查看更多
Explosion°爆炸
6楼-- · 2019-02-21 14:01

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.

查看更多
倾城 Initia
7楼-- · 2019-02-21 14:01

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

查看更多
登录 后发表回答