Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if an XMLHttpRequest failed due to a network issue?

In web programming (JavaScript, Dart, etc), how can I tell if my XMLHttpRequest (aka AJAX) request failed because of a network error?

I define network error as anything like DNS, TCP, connection issues, etc.

Thanks!

like image 506
Seth Ladd Avatar asked Jan 20 '14 18:01

Seth Ladd


People also ask

What is status in XMLHttpRequest?

The read-only XMLHttpRequest. status property returns the numerical HTTP status code of the XMLHttpRequest 's response. Before the request completes, the value of status is 0. Browsers also report a status of 0 in case of XMLHttpRequest errors.

What is XMLHttpRequest status 0?

status is 0 when your html file containing the script is opened in the browser via the file scheme. Make sure to place the files in your server (apache or tomcat whatever) and then open it via http protocol in the browser.

What is XMLHttpRequest error?

This error is a Javascript error and it can be inspected from AndroidStudio terminal or from the browser console. If you run into this problem it means that the requests to the API server are failing due to a CORS error.


1 Answers

In Dartium, and in Chrome JS at least, you can detect the failure by seeing that you hit readyState == 4 ("done") with a status of zero. Zero is not a valid server response, all HTTP server responses are above 100, so it means that it didn't actually reach the server (or at least the server didn't speak proper HTTP).

The onError stream will also get a progress event at this point.

In synchronous mode, the error will be thrown instead.

like image 52
lrn Avatar answered Oct 18 '22 19:10

lrn