AJAX not working on unload
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);
精彩评论