我正在使用ajax函数将整个数据更新到solr中。它正在运行ok bt我想添加一个取消按钮,以便用户可以取消之间的索引,这里是我的代码
$(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);
}
答案 0 :(得分:0)
您可以尝试以下代码
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();
})
答案 1 :(得分:0)
答案 2 :(得分:0)
创建ajax的引用变量,如
var ajax = $.ajax({.... });
并在需要时触发abort()
ajax.abort();