避免以前的ajax请求在jquery中再次调用相同的ajax请求

我想避免以前的ajax请求再次在jquery中调用相同的ajax请求

 function userName(e) { $.ajax({ type:"POST", url:BasePath + "/users/userName", data:"name=" + $.trim($(e).val()), success:function (resp) { if (resp == 1) { $('#div.error_message').html('Username already taken...').show(); } else { $('#div.error_message').hide(); } } }); } 

你可以使用.abort()

   

使用$ .post代替$ .ajax,因为它是简化forms的$ .ajax

 var ajaxReq; function userName(e) { if(ajaxReq){ ajaxReq.abort(); //OR you may return from here as //another request is already in progress } ajaxReq = $.ajax({ type:"POST", url:BasePath + "/users/userName", data:"name=" + $.trim($(e).val()), success:function (resp) { if (resp == 1) { $('#div.error_message').html('Username already taken...').show(); } else { $('#div.error_message').hide(); } ajaxReq=null; } }); }