AngularJS Adding a header to the $http.get

前端 未结 2 1687
有刺的猬
有刺的猬 2021-01-21 11:57

I\'m trying to add the \'Authorization\' header containing a token for future HTTP request. Retrieval of the token seems to be fine however when making a get request it fails wi

相关标签:
2条回答
  • 2021-01-21 12:08

    I'm not sure I understand your problem completely as you did not provide what you call

    httpConfig

    If you're struggling to declare the headers, try making the get request like this:

    $http({
      method: 'GET',
      url: YOUR_URL,
      headers: {
        'Content-Type': 'application/json',
        'Authorization': AUTH_STRING_HERE
      }
    }).then(function (response) { ... });
    

    You can add any headers you like in the headers object there.

    0 讨论(0)
  • 2021-01-21 12:29

    Try adding this in your config block and set the token in your rootscope

    $httpProvider.interceptors.push({
        request: function (config) {
            config.headers.Authorization = $rootScope.token;
            return config;
        }
    })
    
    0 讨论(0)
提交回复
热议问题