Search code examples
javascriptajaxbackbone.js

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
    }
});

Solution

  • 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
        }
    });