开发者

AJAX not working on unload

开发者 https://www.devze.com 2023-03-26 07:13 出处:网络
I am having issues doing an ajax request started by onunload/unload. It doesn\'t work. Here is the code:

I am having issues doing an ajax request started by onunload/unload. It doesn't work. Here is the code:

<script>
function ajax (url)
{
    var a;
    try
    {
        a = new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            a = new ActiveXObject ("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            alert ("browser fail");
        }
    }
    a.onreadystatechange = function ()
    {
        alert(url + " : " + a.readyState);
        if(a.开发者_开发百科readyState == 4)
        {
            var info = a.responseText;
            //alert(url + " : " + info);
        }
    }
    a.open("GET", url, true);
    a.send(null);
}
</script>
<body onunload="ajax('ex.php')">

</body>

Basically the page unloads before the ajax can finish it's stuff. Does anyone have any solutions?


Use a synchronous XHR request.

   req.open("GET", url, false);
0

精彩评论

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