简体   繁体   English

EmberJS / Ember Data自定义http标头

[英]EmberJS / Ember Data customize http header

using ember 1.13.2 with ember data 1.13.4 in conjunction with a rails backend. 结合使用ember 1.13.2和ember数据1.13.4以及rails后端。

according to the ember guides, the following statement should modify the http headers for every XHR request: 根据余烬指南,以下语句应为每个XHR请求修改http标头:

App.ApplicationAdapter = DS.RESTAdapter.extend({
  headers: {
    "Authorization": "secret key"
  }
});

so I added this as coffee script to my application 所以我把它作为咖啡脚本添加到了我的应用程序中

App.ApplicationAdapter = DS.RESTAdapter.extend
  headers: {
        "Authorization": "foofoo"
      }

and it's included and correctly compiled to javascript. 并已将其正确编译为javascript。

But the http header isn't extended with the new header at all. 但是http标头根本没有扩展为新标头。

http://discuss.emberjs.com/t/passing-header-information-to-rest-get-request-using-restadapter/4220/8 http://discuss.emberjs.com/t/passing-header-information-to-rest-get-request-using-restadapter/4220/8

The only solution that works is the one with configuring jquery ajax: 唯一有效的解决方案是配置jquery ajax的解决方案:

Ember.$.ajaxPrefilter(function(options, oriOpt, jqXHR) {
    jqXHR.setRequestHeader("someHeader", "someValue");
});

But I would prefer the ember way 但我更喜欢灰烬的方式

Update : 更新

Strange, it now just works with the ember way. 奇怪的是,它现在只适用于余烬方式。 just restarted my computer. 刚重启我的电脑。 maybe this was a browser cache issue. 也许这是浏览器缓存问题。

As per the Ember docs, you seem to be mostly correct. 根据Ember文档,您似乎基本上是正确的。 Here's the solution that ought to work, courtesy of Ember documentation 这是应该起作用的解决方案,由Ember文档提供

Some APIs require HTTP headers, eg to provide an API key. 一些API需要HTTP标头,例如以提供API密钥。 Arbitrary headers can be set as key/value pairs on the RESTAdapter's headers property and Ember Data will send them along with each ajax request. 可以在RESTAdapter的headers属性上将任意标题设置为键/值对,Ember Data会将它们与每个ajax请求一起发送。

App.ApplicationAdapter = DS.RESTAdapter.extend({
  headers: {
    'API_KEY': 'secret key',
    'ANOTHER_HEADER': 'Some header value',
    'AUTHORIZATION': 'secret key'
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM