2010-12-15 72 views
0

当用户试图离开时,必须执行一个代码只是一个简单的调用服务器通知必须删除一个文件,它不作任何回应,但系统不'打电话。在卸载函数执行AJAX调用

    $(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来,但结果是一样的......

+0

你得到一个JavaScript错误?警报(1)后您需要分号。 – Chocula 2010-12-15 18:43:20

+0

我没有什么多数民众赞成在问题... – hidura 2010-12-15 21:25:48

回答

0

AJAX是默认异步的,并且将与文件卸载结束。尝试将其作为同步呼叫运行。

你必须改变这

phone.open(method, address, false /*synchronous*/); 

和页面应该等待请求完成。

+0

但页面不会等待响应? – hidura 2010-12-15 22:23:40

+0

它应该这样做。同步意味着脚本将等待直到请求完成。只要你不返回,文档仍然应该是活动的。 – wazz3r 2010-12-15 23:08:16

1

太好了,我和Magento Checkout有同样的问题。我解决了这个方式

var responseCancel= jQuery.ajax({ 
     url:'<?php echo Mage::getBaseUrl() . 'checkout/onepage/callWsCancelOrder'; ?>', 
     cache: false, 
        async: false 
     }).responseText; 

谢谢