Receive response headers after spinal sampling is complete

I need to read the response headers in an Ajax request made by the backbone.js fetch method. is there any way to read the 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
    }
});
+4
source share
4 answers

Use the success callback to get the xhr object, so you will be able 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
    }
});
+16
source

Backbone fetch() jqXHR object. done() , , , . getResponseHeader() jqXHR, , getAllResponseHeaders(), .

, fetch() - :

var jqXHR = Backbone.Collection.prototype.fetch.call(this, options);
jqXHR.done(function() {
    // Get all headers:
    console.log('All headers:', jqXHR.getAllResponseHeaders());
    // Or get a specific header:
    console.log('Content-Length:', jqXHR.getResponseHeader('Content-Length'));
});
+3

var CustomPageCollection = Backbone.Collection.extend({
    model: CustomPage,
    url: '/pb/editor/pages',
    parse: function(resp, xhr) {
        this.paginationInfo = JSON.parse(xhr.getResponseHeader('X-Pagination-Info'));
        return resp.items;
    }
});
0

: "" BakcboneJs - Parse

parse:function(a,b,c){      
    console.log("a",a);
    console.log("b",b);
    console.log("c",c);
},

b:)

-1

All Articles