开发者

Execute an AJAX call on unload function

I have to execute a code when the user try to leave is just a simple call to the server informing that have to delete a file, it doesn't make any response but the system don't call.

                    $(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", 'applicati开发者_StackOverflowon/x-www-form-urlencoded');
        phone.send(reqStr);

I've try with onbeforeunload to but the result was the same...


Great, i had the same problem with the Magento Checkout. I solved this way

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

Thank you


AJAX is asynchronous by default and will end with the document unload. Try running it as a synchronous call instead.

You have to change this to

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

and the page should wait for the request to finish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜