Javascript 在页面卸载时可靠地完成Ajax请求
我有一个html/javascript应用程序。当用户刷新页面或关闭页面时,我需要发出ajax请求,然后关闭应用程序 为此,我正在使用页面卸载事件 我打电话来 window.onbeforeunload=beforeFunction before函数将发出ajax请求。但是当我签入fiddler时,我没有看到ajax请求。但是,如果我调试应用程序并使用f10执行每一行,那么我会在fiddler中看到ajax请求 这就是我的ajax请求的形成方式:Javascript 在页面卸载时可靠地完成Ajax请求,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个html/javascript应用程序。当用户刷新页面或关闭页面时,我需要发出ajax请求,然后关闭应用程序 为此,我正在使用页面卸载事件 我打电话来 window.onbeforeunload=beforeFunction before函数将发出ajax请求。但是当我签入fiddler时,我没有看到ajax请求。但是,如果我调试应用程序并使用f10执行每一行,那么我会在fiddler中看到ajax请求 这就是我的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);
}
});
试试这个…请让js fiddle成为一个对卸载问题毫无帮助的小提琴?谢谢@olly_uk。其实我不知道
$(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);
}
});
});