2

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:

http://photorankapi-a.akamaihd.net/streams/1537083805/media/photorank?auth_token=11ff75db4075b453ac4a21146a210e347479b26b67b1a396d9e29705150daa0d&version=v2.1

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.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • Can you do a string match for 404 on req.responseText? I looked at the MSDN documentation and this Event does not seem to expose error codes. – andersevenrud May 08 '14 at 23:27
  • Actually that's the problem here, if any error occurs, the req.responstText for XDomainRequest will be empty,I check the string req.responseText == "" is true, which means no difference between different errors. – user3618409 May 08 '14 at 23:42
  • Sorry, missed that one. As idehold says in the answer below, it seems you are out of luck :/ – andersevenrud May 08 '14 at 23:45

1 Answers1

2

Unfortunately the XDomainRequest object does not provide anyway of accessing the response HTTP status code. The only properties you can access on the XDR instance are: constructor, contentType, responseText, and timeout; and the only methods are: abort(), open(), and send(). On top of that, the error handler doesn't even get passed any arguments.

idbehold
  • 16,833
  • 5
  • 47
  • 74
  • So any alternative way to make the cross domain request? We use dojo in the project, but seems like jsnop call using dojo.io.script with callbackParam:"callback" and url: [JSNOPLINK](https://photorankapi-a.akamaihd.net/customers/215934/streams/bytag/henry-sofa-linen-weave-h785?auth_token=11ff75db4075b453ac4a21146a210e347479b26b67b1a396d9e29705150daa0d&version=v2.1&callback=dojo.io.script.jsonp_dojoioscript1._jsonpcallback) doesn't work. – user3618409 May 08 '14 at 23:52
  • You can have the server respond with 200 even for validation (structured) errors and then have it expose the status in the response in some other way than a header. – Dtipson Jun 14 '15 at 18:36