0

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?

apsillers
  • 112,806
  • 17
  • 235
  • 239
Stacked
  • 841
  • 2
  • 12
  • 23

1 Answers1

1

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.

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • That worked like a charm, however I am having problem passing the retrieved value from bg script using `function doXHR(url) { var request = kango.xhr.getXMLHttpRequest(); request.open('HEAD', url); request.send(null); request.onload = function () { if (request.status == 200) { return request.getResponseHeader("Content-Length"); } }; request.onerror = function () { kango.console.log("Check url, something went wrong"); } };` to caller in content-script `kango.invokeAsync('doXHR', url, function (size) { kango.console.log(size); });`? – Stacked Nov 19 '13 at 10:56