Adding a custom header to HTTP request using angular.js

前端 未结 8 1771
说谎
说谎 2020-11-27 13:01

I am a novice to angular.js, and I am trying to add some headers to a request:

   var config = {headers: {
            \'Authorization\': \'Basic d2VudHdvcnR         


        
相关标签:
8条回答
  • 2020-11-27 13:42

    If you want to add your custom headers to ALL requests, you can change the defaults on $httpProvider to always add this header…

    app.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.headers.common = { 
            'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
            'Accept': 'application/json;odata=verbose'
          };
    }]);
    
    0 讨论(0)
  • 2020-11-27 13:44

    And what's the answer from the server? It should reply a 204 and then really send the GET you are requesting.

    In the OPTIONS the client is checking if the server allows CORS requests. If it gives you something different than a 204 then you should configure your server to send the correct Allow-Origin headers.

    The way you are adding headers is the right way to do it.

    0 讨论(0)
提交回复
热议问题