1

I am using a ajax function to update the whole data into the solr . it is running ok bt i want to add a cancel button so that the user can cancel the indexing in between here is my code

$(document).ready(function()
  {
    var callback=getQuerystringNameValue('auth_callback');
    if(callback==1)
    {
        $('.loder').fadeIn('slow');
        $.ajax({

                type:"post",
                url: 'auth.php',
                data: 'indexing=true',
                success: function(data) 
                {
                    $('.loder').fadeOut('slow');
                    window.location=<?php Home; ?> ;
                }
            });
    }

  });

if(isset($_POST['indexing']))
    {
     handle_dropbox_auth($dropbox);
        $user=$dropbox->GetAccountInfo();
         $user_id=$user->uid;
       handle_dropbox_auth($dropbox);
         $files = $dropbox->GetFiles();

       foreach ($files as $key => $value) 
        {

           if($value->is_dir!=1 && !in_array($value->mime_type, $restrict))
           {
               //print_r($value);

            index_into_solr($value,$dropbox,$client,$user_id);
           } 
       }

      }  

this is the function to index the files into solr

function index_into_solr($value,$dropbox,$client,$user_id) 
                        {
                               $file= end(explode('/',$value->path ));
                             $fileMetadata = $dropbox->DownloadFile($value->path,'download/'.$file);

                              $content=file_get_contents('download/'.$file);
                              $update = $client->createUpdate();
                              $doc1 = $update->createDocument();
                              $ext=explode('.',end(explode('/', $value->path)));
                              array_pop($ext);

                              $doc1->id = $value->rev;
                                $doc1->name =  implode('.', $ext) ;
                                $doc1->content = $content;
                                $doc1->author=$user_id;
                                $doc1->sku=$value->path;
                              $update->addDocuments(array($doc1));
                              $update->addCommit();

                            //this executes the query and returns the result
                            $result = $client->update($update);
                            unlink('download/'.$file);
                        }
john
  • 567
  • 8
  • 23

3 Answers3

0

You can try below code

var xhr = $.ajax({
    type:"post",
    url: 'auth.php',
    data: 'indexing=true',
    success: function(data) 
    {
        $('.loder').fadeOut('slow');
        window.location=<?php Home; ?> ;
    }
});
// Cancel button click
$('.btn-cancel').click(function(){
//kill the request
    xhr.abort();
})
Vikram
  • 675
  • 6
  • 25
0

Try using .abort(), look this link and this. Hope that help.

Community
  • 1
  • 1
0

make a reference variable of ajax like

var ajax = $.ajax({.... });

and trigger abort() when needed

ajax.abort();
Ashisha Nautiyal
  • 1,389
  • 2
  • 19
  • 39