Abort AJAX request

Asked

Viewed 761 times

8

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

On the client side I use the abort()

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

if(requisicao && cliente_abortou_requisicao){ 
    requisicao.abort(); 
}

Now on the server side I’m doubtful. How do I stop PHP from running? Because if I only do it on the client side, script still runs on the server and if I make another AJAX request to the server this request is waiting for the end of the previous request.

Example:

 <?php

      $DB = getConexaoOracle();

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

      $array_de_retorno = array();

      while($RS->hasNext()){
            ...
      }

      die(json_encode($array_de_retorno));

 ?>

Would anyone know how to treat that this session was aborted by the client?

  • 3

    Ajax requests last between 0.1 and 1 second in 99.9999% of cases. What is the scenario that needs this feature? I am not saying that it is not valid, but it would be interesting to know the problem to help better in the solution.

  • I am using an asynchronous AJAX request to carry out the printing of an order report. which, depending on the Representative, can take up to 5 minutes. But sometimes they don’t want to wait.

  • This may help, take a look. http://stackoverflow.com/questions/16810339/how-to-cancel-a-php-process-when-ajax-call-is-cancelled

  • 1

    Test add "async: false" to your ajax call.

  • This does not seem to be the right way to try to solve it. Perhaps this other question gives you some light: http://answall.com/questions/164938

  • Daniel, caching the result would not be good for me because I use price rules. A X product may have a different price per N variable ( status, tax classification, category, payment condition, etc.), for example, a 1 product query takes on average 1 second, If the user wants to search the products of a Product Line that can have up to 3,000 products if it does not specify a better filter, the system will process and get stuck in the request for 3,000 seconds. I wish I could give him the option to cancel the request and redo the filter more quickly.

Show 1 more comment

3 answers

2


Let’s go by step of the problem, your solution to the problem (abort the ajax on the client side) is not the actual solution, the problem is on the server-side.

How do I stop PHP from running?

PHP by default interrupts the request whenever the user gives up the page. The only codition for this not to occur is if you set a ignore_user_abort(true) or if you set the value in ignore_user_abort = 1 in the 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 you use the mysqli_*, for example, but should also be applicable to your case, this happens to be outside of PHP. Basically PHP no longer has control over it directly. But, after back to the PHP command the process will be killed, understand that when you enter $array_de_retorno process will be killed because user aborted.

But, because if I make another request it awaits previous request?

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

But, let’s face it, when you do it:

session_start();

Will block writing and recording in session, which is a mere archive, this will cause any other request to be blocked.

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

See this answer about this same problem.

  • Hello, I did a test but it’s still not stopping PHP, am I doing it wrong? follow the example AJAX ( https://jsfiddle.net/h6n8k4o8/1/ ).

  • The fopen is editing the same file, this is exactly the same situation as the session. The session, as written above is only a file, where only one process can edit it. The fopen is doing EXACTLY the same thing as Session, ie two requests to edit the same file. If you make a request to edit teste and another to edit the same testeit will wait for the first to finish, because can not two processes edit the same file.

  • Utilize fopen(bin2hex(random_bytes(5)).'.txt','w'); and see if the problem persists, it will make each process write a different file, thus solving the problem.

0

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 the execution continues on the server. You would need to restructure your back code to verify if you could continue execution or not.

  • Could you give me an example? Using the above example.

  • For your example, I find it simpler and indicated that you use pagination with a button just below the listing (click more). I think it would be much more feasible. It would be quick and there would be no need to cancel the request.

-3

  • Could you give me an example? Using the example quoted. ?

  • Use after the last line you would like to run

  • I still don’t understand. I send a request to the server. I store this request in a variable in JS. When the client aborts the request I simply pick up the variable and call the abort() médoto. But there on the server still running, and the server does not accept another request while processing. And with that I can’t send any variable via GET or POST telling the server that the connection was interrupted by the client. Got it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.