Javascript 对unload函数执行AJAX调用
当用户试图离开时,我必须执行一个代码,这只是对服务器的一个简单调用,通知必须删除一个文件,它不会做出任何响应,但系统不会调用Javascript 对unload函数执行AJAX调用,javascript,ajax,jquery,Javascript,Ajax,Jquery,当用户试图离开时,我必须执行一个代码,这只是对服务器的一个简单调用,通知必须删除一个文件,它不会做出任何响应,但系统不会调用 $(window).unload(function(){ alert(1) window.key = document.getElememtsByTagName('body')[0].getAttribute('key');
$(window).unload(function(){
alert(1)
window.key = document.getElememtsByTagName('body')[0].getAttribute('key');
catcher('clearBt');//The catcher makes the AJAX call like i show below.
});
//This is the call to the server.
phone.open(method, address, true);
phone.setRequestHeader("Content-type", 'application/x-www-form-urlencoded');
phone.send(reqStr);
我尝试了onbeforeunload,但结果是一样的…默认情况下AJAX是异步的,将以文档卸载结束。尝试将其作为同步调用运行 你必须把这个改成
phone.open(method, address, false /*synchronous*/);
页面应该等待请求完成。太好了,我在Magento结账时也遇到了同样的问题。我就是这样解决的
var responseCancel= jQuery.ajax({
url:'<?php echo Mage::getBaseUrl() . 'checkout/onepage/callWsCancelOrder'; ?>',
cache: false,
async: false
}).responseText;
var responseCancel=jQuery.ajax({
url:“”,
cache:false,
异步:false
}).responseText;
谢谢您是否收到JavaScript错误?在警报(1)之后需要一个分号。我没有任何问题…但是页面不会等待响应?它应该这样做。同步意味着脚本将等待直到请求完成。只要不返回文档,文档就应该仍然处于活动状态。