Search code examples
ember.jsember-dataember-cli

Add parameter to every API request


I need to add an authentification-token to every request I send to my JSON API.

How can I add it via the Adapter?


Solution

  • You could use jQuery.ajaxPrefilter (http://api.jquery.com/jquery.ajaxprefilter/)

    Here is an example: http://log.simplabs.com/post/57702291669/better-authentication-in-ember-js#gist6499654

    Ember.$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
      if (!jqXHR.crossDomain) {
        jqXHR.setRequestHeader('X-AUTHENTICATION-TOKEN', App.Session.get('authToken'));
      }
    });
    

    Add this code to an initializer or create a new one for this (e.g. ajax-initializer).