在页面上可靠地完成Ajax请求的卸载 [英] Reliably complete Ajax request on page unload
问题描述
我有一个html/javascript应用程序.当用户刷新页面或关闭页面时,我需要发出Ajax请求,然后关闭应用程序.
I have a html/javascript application. When user refreshes the page or close the page i need to make a ajax request and then close the application.
我正在为此目的使用页面卸载事件.
I am using page unload event for this purpose.
我在打电话 window.onbeforeunload = beforeFunction;
I am calling window.onbeforeunload=beforeFunction;
beforeFunction将发出ajax请求.但是,当我签到提琴手时,我看不到ajax请求.但是,如果我调试应用程序并用f10执行每一行,那么我会在提琴手中看到ajax请求.
beforeFunction will make the ajax request. But when i check in fiddler i dont see the ajax request. However if i debug the application and execute each line with f10 then i see the ajax request in fiddler.
这就是我的ajax请求形成的方式:
thats how my ajax request is formed:
$.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);
}
});
推荐答案
$(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);
}
});
});
尝试一下....
这篇关于在页面上可靠地完成Ajax请求的卸载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!