Abort AJAX request


I have a question. I need to cancel an AJAX request on the client side and on the server side.

On the client side I use abort()

var requisicao = $.ajax({ url : 'xyz.php'  });

if(requisicao && cliente_abortou_requisicao){ 

Now on the server side I'm doubtful. How do I stop PHP from executing? For example, if you just do the client side, the script still runs on the server and if I make another AJAX request to the server this request is waiting for the previous request to finish.



      $DB = getConexaoOracle();

      $sql = "SELECT * FROM FUNCIONARIOS"; // 1 milhão de registros (exemplo)
      $RS = $DB->Execute($sql);

      $array_de_retorno = array();




Would anyone know how I can handle this session being aborted by the client?

asked by anonymous 07.11.2016 / 19:05

3 answers


Let's go through the problem step, your solution to the problem (abort ajax on the client side) is not the solution indeed, the problem is on the server side.

How do I stop PHP from executing?

PHP by default stops the request every time the user drops the page. The only coding for this does not occur if you set a ignore_user_abort(true) or if you set the value in ignore_user_abort = 1 in php.ini .


If the remote client disconnects, the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button.

The problem is that when using mysqli_* , for example , but it should also be applicable to you, this happens outside of PHP. Basically PHP no longer has control over this directly. But after returning to the PHP command the process will be killed, understand that when you enter $array_de_retorno the process will be killed because the user aborted.

But, if I make another request, does it wait for the previous request?

This is probably the use of SESSION or ISOLAMENTO (and also the LOCKING of tables) of the database used. The first situation is extremely more likely in your case (and in most cases). There is also the possibility of defining some rate limit , which defines how many requests can be made per client, this can be defined in Apache / Nginx (and related).

But, let's most likely, when you do this:


It will block writing and writing to the session, which is a mere file , this will cause any other request to be blocked.

One solution is to use session_write_close() and thus allow another request read and write the data in the session file, because when you call the session_write_close() indicates that the process will no longer write anything, but you can still read

See this answer about this same problem.

28.01.2017 / 11:11

When you make a request on the front, it is sent to the server, which executes the operation, and when you cancel before receiving the return, you only stop receiving it, but execution continues on the server. You would need to restructure your back code to see if it could continue running or not.

28.12.2016 / 16:44

Try adding a simple break ( link ), or exit (< a href="http://php.net/manual/en_US/function.exit.php"> link ) in the code php

28.12.2016 / 14:22