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!
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With