Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get response headers after backbone fetch is complete

I need to read response headers in an Ajax request made by backbone.js fetch method. is there any way to read headers if I override the fetch method:

var PageCollection = Backbone.Collection.extend({

    url: 'http://localhost/cms?_mn=Mod_Admin&_mf=getAllPages',

    model: PageModel,

    fetch: function (options) {
        Backbone.Collection.prototype.fetch.call(this, options);
        // The above line of code works and fetch the dataset 
        // BUT how i can read the response headers at this point
    }
});
like image 497
asim-ishaq Avatar asked Dec 02 '22 17:12

asim-ishaq


1 Answers

Use "success" callback to get the xhr object, so you will have an ability to get all the response headers:

collection.fetch({
    success: function (collection, response, options) {
        options.xhr.getAllResponseHeaders(); // To get all the headers
        options.xhr.getResponseHeader('header_name'); // To get just one needed header
    }
});
like image 177
Nazar Avatar answered Dec 04 '22 05:12

Nazar