我的要求是,我在我的项目中编写了很多 $.post 方法。我想检查每个请求中的会话,并根据会话值,我想处理我的页面。但我不想修改会话的所有方法($.post),所以我想检查“中的会话” ajaxStart 或 ajaxSend”,并且根据响应我想中止主 AJAX 调用。
我用我的一点知识进行了尝试。下面的示例代码不起作用,因为 $.post 是异步的。任何人都可以帮助我在 ajaxStart 或 ajaxSend 状态中止 AJAX 请求吗?
var pp;
link=function(id){
var url="http://localhost/jqueryui/test.php";
pp=$.post(url,{"id":id},function(data){
$("#div"+id).html(data);
});
}
$(function(){
$("#loading").ajaxStart(function(){
alert("Hi start");
$(this).show();
var url="http://localhost/jqueryui/test.php";
$.post(url,{"id":99},function(data){//checking the session
if(data == "close")
pp.abort();
});
});
});
Run Code Online (Sandbox Code Playgroud)
您可以通过在回调中使用第二个参数“jqXHR”来中止请求:
$( document ).ajaxSend( function( event, jqXHR, ajaxOptions )
{
jqXHR.abort(); // aborts the ajax request
} );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3574 次 |
| 最近记录: |