Send AJAX results but continue processing in PHP

I use AJAX to update some values ​​in the database. Everything did a great job with this, but now I would like to implement some entries. The logging functions look like they are going to spend a lot of time processing, and they have no reason why the user will have to wait for them to finish in order to see their AJAX results.

So, I'm trying to find a way to send AJAX results and continue processing on the server side. My research has called the ignore_user_abort function, but apparently I am not using it correctly.

This guide is that I disabled my code.

Here is my javascript (jQuery):

$.ajax({ type: "GET", url: "ajax.php", data: { "mydata": mydata }, success: function(msg) { $("span#status").fadeOut(200, function() { $("span#status").html(msg); $("span#status").fadeIn(200); }); } }); 

And my PHP:

  $response = "This is my response"; //Begin code from link ob_end_clean(); header("Connection: close"); ignore_user_abort(true); ob_start(); echo $response; header("Content-Length: " . mb_strlen($response)); ob_end_flush(); flush(); //End code from link echo "I should not see this text"; 

Unfortunately, I see this text after a flash ();

Any ideas?

Update - Bugfix: I found out my error. After copying the words into the tone of the various sentences of the code, I decided that this must be a mistake in my apache / php configuration. Turns out I need to add two lines in order to get apache not to buffer my results:

 apache_setenv('no-gzip', 1); ini_set('zlib.output_compression', 0); 
+4
source share
2 answers

Well, you would see it, right? You said this echo!

In all seriousness, your example should work. It seems that the code is executed after the flash, so you can register if the flash sent a request to the browser.

0
source

The PHP script cannot tell the browser to close the connection and not wait for additional data. Using flush(); only sends the current output in a chain to the web server, but does not guarantee that it will immediately arrive in the browser. The web server can cache the output until the PHP script completes execution.

As OP wrote in the last paragraph, the solution is to configure Apache to not buffer the results.

0
source

Source: https://habr.com/ru/post/1333070/


All Articles