Backbone 获取请求的 js 设置 header

Backbone js setting header for get request

如何为我的令牌设置 header 以向外部 url 发送 GET 请求? 我实际上不想覆盖 Backbone.sync 我想手动设置它来获取我的 collection 我应该把 header 放在哪里?

具体问题: 我想发送 GET 请求并且我有特定的 URL 我应该为此设置令牌我应该怎么做?

您可以将 optionsheaders 参数中的自定义 headers 提供给 collection 的 fetch 方法:

var ThingCollection = Backbone.Collection.extend({
  url: '/things'
});

var thingCollection = new ThingCollection();
thingCollection.fetch({ 
  headers: {
    'Authorization': 'Basic NAME:PASSWORD'
  } 
});