Stacked
Stacked

Reputation: 861

How to retrieve response-headers from a cross-domain HEAD request from my cross-browser addon?

I am using Kango framework to develop a cross-browser addon, using the following code I am making HEAD request's utilizing Kango.XHR which is getting successfully executed (as shown in HTTP DEBUGGER) and the code below in my background script also returns data.status == 200.

function doXHR(url) {
var details = {
    method: 'HEAD',
    url: url,
    async: true
};


kango.xhr.send(details, function(data) {
    if (data.status == 200) {
        kango.console.log(data.status);
    }
    else { // something went wrong
        kango.console.log('something went wrong');
    }
});
};

Now, I want to get the value of Content-Length response header from the above but have no clue how to do that?

Upvotes: 0

Views: 383

Answers (1)

apsillers
apsillers

Reputation: 115980

Ordinarily, you can read response headers with xhr.getReponseHeader. Depending on how Kango works, that method may already be available on your data object.

If data.getReponseHeader("Content-Length") doesn't work, then instead of using kngo.xhr.send, you might try imitating a normal Ajax call with kango.getXMLHttpRequest:

var request = kango.xhr.getXMLHttpRequest();
request.open('HEAD', url);
request.send(null);
request.onload = function() {
    if(request.status == 200) {
        console.log(request.getResponseHeader("Content-Length"));
        console.log(request.responseText);
    }
}
request.onerror = function() {
    console.log("something went wrong");
}

Again, depending on how Kango operates under the hood, your server might need to serve an Access-Control-Expose-Headers response header to allow the client to read it. This won't likely be necessary, since extension code usually is not bound by the same origin policy, but I offer it only to help you iron out any possible inconsistencies in Kango's cross-platform API.

Upvotes: 1

Related Questions