i am currently working on a CRM system with a huge database. If the user wants to search a customer, he can use the ajax search. Everytime he changes something in the search field, while a call is pending, the old call is cancelled and a new one will be send to the server. My problem is, that the php processes on server side continue running. So if the user starts typing in an address, several requests are started and cancelled and the server needs more and more time to answer.
Is is possible to cancel the running php process, when the ajax call is cancelled? Or is there a possible solution on server side, to detect if a search request from the same user is running and cancel it before starting the new one?
Thanks in advance for your answers.
Sometimes you might want to cancel the ajax request that is pending from long time or want to cancel request if user accidently clicked submit button. In that cases, so you can just use abort () method. If the request has been sent already, this method will abort the request. See the below example.
In order to cancel a request, we need a cancel token, which is a reference to the request call. If we store it in a variable, we will be able to use it to cancel the request whenever a new one is triggered.
Step 1. First of all check request is AJAX request or not. Step 2. Check Referer : Give response only when AJAX request is from your own url Step 3. Use Post Method in AJAX Always Use Post method while sending request and check $_POST variable before giving response
So, in this tutorial we will see how to secure ajax request or how to make secure Ajax call Step 1. First of all check request is AJAX request or not. Step 2. Check Referer : Give response only when AJAX request is from your own url Step 3. Use Post Method in AJAX
Your server side php has to output something to detect the request is canceled. But as you are likely performing a lengthy sql query, you can't output anything.
But still you can save your connection id into a session, and kill the connection if it's detected:
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