由于ajax是异步的,因此在正确发送响应之前将卸载页面,从而有效地终止连接。尝试设置
async:false;,尽管这会延迟卸载页面,直到接收到响应之后,如果您的服务器运行缓慢,这对于用户体验来说并不好。
$(window).unload( function () { test(); }); function test() { alert("Hi"); $.ajax({ async: false, type: "POST", url: baseUrl + 'Index/test', data: "user_id=" + "Nisanth" + "& chat_id=" + 2, success: function(msg){ alert(msg); } }); alert('Success'); }


