Search code examples
backbone.jsgetheaderrequesttoken

Backbone js setting header for get request


How can I set a header for my token to send GET request to an external url? I don't want to override the Backbone.sync actually I want to manually set it for fetching my collection where should I put the header?

Specific question: I want to send GET request and I have specific URL I should set the token for that how should I do that?


Solution

  • You can give custom headers in the headers parameter in the options to the collection's fetch method:

    var ThingCollection = Backbone.Collection.extend({
      url: '/things'
    });
    
    var thingCollection = new ThingCollection();
    thingCollection.fetch({ 
      headers: {
        'Authorization': 'Basic NAME:PASSWORD'
      } 
    });