I have an AJAX form where users can upload CSV files. These files are rather small and server-side processing will take much more time than the actual file upload. Nevertheless, the upload might still take a second or two, depending on file size and connection speed. Furthermore, it is important for the user to know if the server-side operation succeeded or failed.
Now I would like to distinguish between "uploading file" and "awaiting response", so I can show corresponding messages/indicator icons etc. to the user.
I'm using jQuery's $.ajax
, where I can specify a complete()
-callback. But this will not fire until the server's response has been received. And as far as I can tell, there is no requestComplete()
-callback.
So, in short, is there a way to detect if an AJAX request has been completely sent to the server, even though the server did not respond yet?
I'd rather not resort to asynchronous processing on the server and client-push (WebSockets or Comet etc.) if possible. (Server runs PHP).
The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.
success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.
The parameter is not being deprecated, the success method is. You can continue using success: function re-read it carefully.
by default async is true. it means process will be continuing in jQuery ajax without wait of request. Async false means it will not go to next step untill the response will come. By default async is true process will be continuing in jQuery ajax without wait of request.
See the Session Upload Progress feature.
When enabled, PHP will store upload progress informations in the user's session. You can then fetch this data by doing some ajax pulling.
Alternatively, APC has a similar feature.
The answer of arnaud576875 applies to the actual file upload (which you say may or may not take seconds). You may need both methods. In general, there is no mechanism in PHP for tracing the progress within a script - neither does PHP know what % it is at nor does it necessarily return any data to the caller until the end (even if it did, your JS would not receive the partial output).
The standard way to do this is to have the browser continually poll the server for the progress of the script. The upload is separate and so you would need to find this also and merge the two numbers - this would require an estimate of how long each task (uploading, processing) would take and some good Algebra skills.
In your script you would need to determine the percentage done (if you have a large foreach loop this is easy) and store it somewhere (database,file,APC). You would need also a way of identifying the user (user_id and/or pass a random number from javascript as an ID).
Your polling script (ex. every 2 seconds) would then get this number for the progress of the script by passing the unique ID(s).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With