Home » Php » Data loss when request get cancelled using AJAX in Laravel

Data loss when request get cancelled using AJAX in Laravel

Posted by: admin October 26, 2017 Leave a comment

Questions:

I’m sending a big amount of data with POST using ajax in Laravel.
When I send request rapidly and redirect the page to another page then data is getting lost when ajax call is canceled or aborted.

I’ve mentioned the process below:

  1. Send ajax request rapidly
  2. Then before responding to latest request of ajax call by browser I redirect to another page
  3. Then I check the data which is storing when ajax call happen. I found the data is lost.

Below is the code

<script>
 var cssData = $('.css-textarea').val();
 $.ajax({
   url: '/save-data',
   type: 'POST',
   data: {cssData: cssData},
   success:function(response){
      //do something after success
   }    
 });
</script>

PHP/Laravel Code

public function saveCssData(){   
   $data = Input::get();
   $cssData =  $data['cssData'];        
   //Do something to save data
    $db->cssData = $cssData;
    $db->save();
}

Please do not assume the exact code because I’ve given an example.
This code works fine but when I send rapid calls by ajax and before getting a response, I redirect to another page then the data is lost. I’ve set enough post_max_size and other configurations but still, it’s not fixed

Example of Input which I am sending by ajax request

{
 "testCss":
  " #class{background:#fff}../*big amount data */...
    #testDiv{color:#000}
   "
}

Example of The Output which I am receiving in database which is lost

{
 "testCss":
  " #class{background:#fff}../*big amount data */...
    #testDiv{col

You can see the data is lost in output. This issue is appearing at sometimes but I noticed that when I request rapidly with ajax and abort the requests by redirecting to another page then it’s appearing but don’t know how to fix this.

Answers: