When making an ajax request, there is a possibility of encountering an error, indicating a failure to establish communication with the intended target (no status code returned).
To handle these errors, you can use the following code:
var oXhr = new XMLHttpRequest;
oXhr.addEventListener("error",function (e) {
//error code
});
These errors typically fall into two main categories:
- A network error due to disconnection
- An issue where the target does not allow ajax requests from different origins (
Access-Control-Allow-Origin
)
Even though Chrome console displays a red error message, accessing the actual error message programmatically proves to be challenging.
I have attempted:
- xhr.responseText
- parsing the event itself
Instead of resorting to pinging my own server to differentiate the network error, I am seeking alternative solutions.