0

I'm using Jquery and php

I would like php to send a response to an ajax call and close the conection but without ending the script treatment on the server.

What i don't want is that the browser wait the end of the server-side script.

The script is used to generate a file on the server, wich takes a lot of time. The ajax call only initiate it and don't need to know the result of the script or wait until the end of it. I want to end the call for that the browser could just forget about it and consider it done.

1) ajax call 2) after checking the call (security and data sended) respond something like "Ok i'll do it". 3) let the script continue on server-side.

Is there a way to do this?

Jonah
  • 13
  • 3
  • What code are you using? Can you show an example. By default $.ajax requests are asynchronous so the script will not wait for it to finish. – Andy Gee Sep 13 '13 at 15:46
  • Yes they are asynchronous BUT the browser (javascript) is waiting for response backward and that's what I don't want to happend. – Jonah Sep 13 '13 at 15:51
  • @AndyGee the asynchronous in this case means that the browser will continue doing any work that is after the ajax call. It does not process the response until the server finishes sending it. – Justin Wood Sep 13 '13 at 15:53
  • My bad I completely misunderstood! Setting `ignore_user_abort` will help with this http://php.net/manual/en/function.ignore-user-abort.php – Andy Gee Sep 13 '13 at 15:57
  • This answer should help you: http://stackoverflow.com/questions/138374/close-a-connection-early – Drahcir Sep 13 '13 at 16:02
  • Seems imposible to send anithing before sending header('conection: close'); – Jonah Sep 13 '13 at 16:10

3 Answers3

0

There is no way of doing this directly. You have an active connection to Apache, or whatever HTTP server you are using (nginx, lightpd, etc), not PHP.

The best way to accomplish something like this would be to setup 'background tasks'. Usually, you will store tasks in a database of some sort and have a background worker, possibly a cron job that executes a script every couple minutes, to check and do any work that is needed.

Justin Wood
  • 9,941
  • 2
  • 33
  • 46
  • I'm using similar things to send emails or do database stuff. Didn't remember of the Redskin, you must be right, Apache will just keep open as long as the script run... Sh.t – Jonah Sep 13 '13 at 15:58
0

The only thing I can think of is to break the job up into smaller parts in PHP.

console.log("Started");
$.ajax({
    url: "/path/to/script/file.php",
    dataType: "json",
    data: {"start":true}
    success: function(data) {
        if(data.status=="OK"){
            console.log("Continuing");
            $.ajax({
                url: "url: "/path/to/script/file.php",
                dataType: "json",
                data: {"continue":true}
                success: function(data) {
                    if(data.status=="OK"){
                        console.log("Completed");
                    }else{
                        console.log("Didn't Complete");
                    }
                }
            });
        }else{
            console.log("Didn't Continue");
        }
    }
});
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
  • OK, have a cron job look for tasks in a database or text file every minute. The first request will just create the tasks and allow the browser to continue. – Andy Gee Sep 13 '13 at 15:59
0

I think this is simple.

The normal process in the php script called by an ajax call is to do everything and then return any data like this:

<?php
    get data passed on POST or GET

    process the data producing a result

    echo $result

    exit;
?>

All you need to do is :

<?php
    echo 'OK';

    get data passed on POST or GET

    process the data

    exit;
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149