I am using CORS to make cross domain call. My problem here is I need to handle the 404 error in XDomainRequest
in IE.
In XDomainRequest
I can only find onerror
event to handle all the error event, but in my requirement I need to show empty placeholder images for 404 error case (The API doesn't provide empty 200 response for this,but use 404) but regardless other errors.
This is the request URL:
In other browsers, I use XMLHttpRequest
and req.status code == 404
to get the responseText
:
{"metadata":{"code":404,"message":"Not Found","version":"v2.1"}}
But When I tried to get the responseText from req.onerror(below), I can get empty string only, so I can not tell the difference from 404 and other errors.
The part of code shows below:
xdr: function (url, method, data, callback, errback) {
var req;
if(typeof XDomainRequest !== "undefined") {
req = new XDomainRequest();
req.open(method, url);
req.onerror = function() {
// I want to handle specific 404 error here
callback(req.responseText);
};
req.onload = function() {
callback(req.responseText);
};
req.send(data);
} else if( typeof XMLHttpRequest !== "undefined") {
req = new XMLHttpRequest();
if('withCredentials' in req) {
req.open(method, url, true);
req.onerror = errback;
req.onreadystatechange = function() {
if (req.readyState === 4) {
if ((req.status >= 200 && req.status < 400) || req.status ==404 ) {
callback(req.responseText);
} else {
errback(new Error('Response returned with non-OK status'));
}
}
};
req.send(data);
}
} else {
errback(new Error('CORS not supported'));
}
}
The XMLHttpRequest works fine and all other browser are working except in IE.