2010-09-09 64 views
3

在下面的代碼中,我使用jQuery ajax()函數來執行longpolling調用。當ajax函數運行時,我點擊鏈接下載Firefox(例如),並停止ajax調用。當顯示下載提示時,jQuery ajax呼叫停止

它實際上就像調用成功,但事實並非如此,它只是停止運行。

這是爲什麼?我想繼續功能...

我在Firefox和Chrome中看到過這個問題。我還沒有測試過其他瀏覽器。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script> 

    <script type="text/javascript"> 

    function longPoller() { 

     $.ajax({ 
      type: "GET", 
      url: '/long-loading-page.php', /* <?php sleep(5000); ?> */ 
      dataType: 'json', 
      async: true, 
      cache: false, 
      timeout:50000, 

      success: function(data){ 

       alert('success'); 
      }, 
      error: function(XMLHttpRequest, textStatus, errorThrown){ 

       alert('error'); 
      }, 

      complete: function() { 

       alert('complete'); 
       } 

     }); 
    } 


    $(document).ready(function(){ 
     setTimeout(function(){ longPoller() }, 1000); //start the longPoller() function 
    }); 

    </script> 
</head> 
<body> 

    <a href="http://download.mozilla.org/?product=firefox-3.6.9&os=win&lang=en-US">download Firefox! (clicking this link will stop the jQuery ajax call... why?)</a> 
</body> 
</html> 

回答

4

點擊下載鏈接即可離開頁面,即使看起來不是這樣。如果沒有文件傳輸,您會看到請求的頁面..嘗試設置target="_blank"或使用iframe作爲鏈接的目標。

+0

謝謝。這兩個選項都有效我想知道爲什麼成功回調觸發。 – koen 2010-09-09 22:25:13

+0

需要確認這些方法在我實際編程之前會起作用,謝謝! – Reimius 2013-04-03 20:18:34