页面卸载时使用 JQuery 的 Ajax 请求 [英] Ajax request with JQuery on page unload

查看:26
本文介绍了页面卸载时使用 JQuery 的 Ajax 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样做:

$(window).unload( function () { 

$.ajax({
    type: "POST",
    url: "http://localhost:8888/test.php?",
    data: "test",
    success: function(msg){
         alert( "Data Saved: " + msg );
    }
}); 
alert (c);
});

但是,从未显示成功警报,该请求似乎也没有到达服务器.我做错了什么?

However, the success alert is never shown, nor does this request seem to be even hitting the server. What am I doing wrong?

推荐答案

我相信您需要使用 async : false 参数使请求同步(默认情况下是异步的).

I believe you need to make the request synchronous instead (it's asynchronous by default) using the async : false parameter.

同步请求会锁定浏览器,直到它们完成.如果请求是异步的,页面只会继续卸载.它足够快,以至于请求甚至都来不及触发.

Synchronous requests lock up the browser until they complete. If the request is asynchronous, the page just keeps on unloading. It's quick enough that the request never even has time to fire off.

这篇关于页面卸载时使用 JQuery 的 Ajax 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆