简体   繁体   中英

EmberJS / Ember Data customize http header

using ember 1.13.2 with ember data 1.13.4 in conjunction with a rails backend.

according to the ember guides, the following statement should modify the http headers for every XHR request:

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.

But the http header isn't extended with the new header at all.

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:

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. Here's the solution that ought to work, courtesy of Ember documentation

Some APIs require HTTP headers, eg to provide an API key. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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