【问题标题】:how to cancel an ajax call如何取消ajax调用
【发布时间】:2014-08-05 11:56:01
【问题描述】:

我正在使用 ajax 函数将整个数据更新到 solr 中。它运行正常,我想添加一个取消按钮,以便用户可以取消之间的索引这是我的代码

$(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);
           } 
       }

      }  

这是将文件索引到 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);
                        }

【问题讨论】:

    标签: php jquery ajax solr


    【解决方案1】:

    你可以试试下面的代码

    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();
    })
    

    【讨论】:

      【解决方案2】:

      尝试使用 .abort(),看看 linkthis。希望有所帮助。

      【讨论】:

        【解决方案3】:

        做一个像ajax一样的引用变量

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

        并在需要时触发 abort()

        ajax.abort();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-14
          • 1970-01-01
          • 2012-01-16
          • 2011-03-12
          • 2016-05-31
          • 1970-01-01
          • 2012-07-30
          相关资源
          最近更新 更多