Angularjs set a authorization header

前端 未结 3 601
Happy的楠姐
Happy的楠姐 2020-12-16 18:01

I am trying to put an authorization header in my requests but it doesn\'t work.

I am using this:

var config = {headers: {
  \'Authorization\': token         


        
相关标签:
3条回答
  • 2020-12-16 18:42

    Try this:

    $http({
    
    url : "127.0.0.1/login",
    method : 'GET',
    headers : {
          Content-Type : 'application/json',    
          Authorization: token
          }
    }).success(function(data){
        alert("login Successfully");
    }).error(function(error){
        alert("login error");
    })
    
    0 讨论(0)
  • 2020-12-16 18:54

    Authorization can't be plain.

    specify weather its a Basic or Bearer Authorization

    Something like

    $http.defaults.headers.common['Authorization'] = 'Basic ' + token;
    

    or

    $http.defaults.headers.common['Authorization'] = 'Bearer ' + token;
    
    0 讨论(0)
  • 2020-12-16 18:58

    I was having this same issue and it turned out the issue had to do with Apache configuration on the server side. One thing I did find that might be useful to you here is that my Authorization token is sent in the preflight request headers rather than the main request, so it might not appear to be in the headers of the request when you look at it in the developer tools.

    I'm setting my defaults like this:

    app.config(function($httpProvider) {
        $httpProvider.defaults.common['Authorization'] = token;
        // For angular 1.5, use:  
        // $httpProvider.defaults.headers.common['Authorization'] = token;        
    });
    
    0 讨论(0)
提交回复
热议问题