Jeremy Miller Jeremy Miller -4 years ago 88
AngularJS Question

How to not always send header in angular to external server

in my app.config i am sending

in header

app.run(function($http) {
$http.defaults.headers.common['MyCode'] = 'buTTon5';
});


Above works great for all my internal web api calls, but I do have some external api calls in which I cannot send them the header , how can I not send it,

Error


XMLHttpRequest cannot load https://us-zipcode.api.smartystreets.com/lookup?zipcode=10001&auth-id=350910987643333. Request header field MyCode is not allowed by Access-Control-Allow-Headers in preflight response.

Answer Source

To delete you need to use interceptors. you will receive config object of $http call. check this updated plunker

interceptors:

app.config([
  '$httpProvider',
  function($httpProvider) {
    $httpProvider.interceptors.push( function() {
      return {
        'request': function(config) {
          if (config.url.indexOf('smartystreets.com') != -1)
            delete config.headers.MyCode; //able to delete with interceptor request method
          return config;
        }
      };
    });
  }
]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download