2014-07-14 46 views
0

我有一個html/javascript應用程序。當用戶刷新頁面或關閉頁面時,我需要創建一個Ajax請求,然後關閉應用程序。在頁面卸載時可靠地完成Ajax請求

我正在使用頁面卸載事件來達到此目的。我打電話給 window.onbeforeunload = beforeFunction;

beforeFunction將發出ajax請求。但是當我檢查小提琴手時,我沒有看到ajax請求。但是,如果我調試應用程序並執行每行f10然後我看到在提琴手的ajax請求。

這就是我的Ajax請求是如何形成的:

$.ajax({ 
      url: url, 
      type: "GET", 
      contentType: "application/json", 
      async: false, 
      crossDomain: true, 
      dataType: 'jsonp', 
      success: function(json){ 
       alert("success: " + json); 
      }, 
      error: function(xhr, statusText, err) { 
       alert("Error:" + xhr.status); 
      } 
    }); 
+0

請JS小提琴 –

+0

小提琴真的不會有問題卸載幫助? –

+0

謝謝@olly_uk。其實我不知道 –

回答

0
$(window).on('beforeunload' function() { 
    $.ajax({ 
     url: url, 
     type: "GET", 
     contentType: "application/json", 
     async: false, 
     crossDomain: true, 
     dataType: 'jsonp', 
     success: function(json){ 
      alert("success: " + json); 
     }, 
     error: function(xhr, statusText, err) { 
      alert("Error:" + xhr.status); 
     } 
    }); 
}); 

試試這個....