AJAX difference between failed and canceled sending a request to another domain

I am making an ajax request to a server in a different domain, but I do not need his reaction, just to find out that he received my request. When everything is in order, the Chrome Developer Tools (Header status) says “canceled” and the console entry “XMLHttpRequest cannot load”, but the server receives my requests. When the server is turned off, the status of the header is not a number, but simply “failure”. Trying to catch this critical difference in JS, I get XHR status 0 in both cases.

+4
source share
1 answer

I am making an ajax request to a server in another domain

You cannot make an ajax request in another domain due to policies of the same origin . You want to look at JSONP, which essentially writes out the <script> for the remote URL.

What is JSONP?

Finding success / error using JSONP calls is difficult, it doesn’t work at all like regular ajax calls. Ideally, you want the remote server to call the callback function on your page as described above.

If you do not control another domain, you can try to detect errors by timeout. Here is a post discussing the jQuery timeout argument for this, although you can certainly implement your own timeout with raw javascript as well.

0
source

All Articles