I have a scenario where I need to make a cross-domain request upon leaving a page. The endpoint has all the proper CORS headers configured.
This is the request:
$.ajax({
type: "POST",
url: URL,
data: {
// some stuff
},
async: false
});
When tested as-is, it works great. When I try to trigger this request from an unload
handler:
$(window).unload(function() {
// same as above
});
it intermittently fails. On the occasion that it fails, I get this stack trace:
Error: A network error occurred.
at Object.x.ajaxTransport.x.support.cors.e.crossDomain.send (http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js:6:9344)
at Function.x.extend.ajax (http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js:6:4804)
at http://my.web.site.com/main.js:129:11
at x.event.dispatch (http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js:5:10006)
at x.event.add.y.handle (http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js:5:6789)
With a jQuery DOMException
and error code 19.
What's going on here? It looks like there's some race condition here that I'm unaware of, but other than setting the request to be synchronous, I'm not sure what else I can do.