2013-11-23 86 views
7

是否可以中止先前運行的Ajax請求?中止以前運行的Ajax請求

var xhr = $.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: "name=John&location=Boston", 
    success: function(msg){ 
     alert("Data Saved: " + msg); 
    } 
}); 
+0

怎麼樣'如果(XHR){xhr.abort();}'? –

+0

我認爲這是可能的,它是可用的ASP.NET AJAX工具包 – Devesh

+0

感謝所有答案。現在它解決了 –

回答

5

嘗試這樣的事情

 $(document).ready(function(){ 
      var xhr; // global object 

      function your_function() { 
       if(xhr && xhr.readystate != 4){ 
        xhr.abort(); 
       } 

       xhr = $.ajax({ 
        type: "POST", 
        url: "some.php", 
        data: "name=John&location=Boston", 
        success: function(msg){ 
         alert("Data Saved: " + msg); 
        } 
       }); 
      } 
     }); 
+0

謝謝,它爲我工作。 –