2017-04-13 88 views
0

我正在與Nodejs和Angular JS一起使用JWT令牌。我發現$ httpProvider在每個請求上設置標題的方式。但標題不會被髮送。我的NodeJS應用程序在訪問該標題時未定義。AngularJS不發送授權或任何額外的鍵/值標頭

我的角碼

有關設置授權令牌 -

$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) { 
     return { 
      'request': function (config) { 
       config.headers = config.headers || {}; 
       if ($localStorage.token){ 
        console.log($localStorage.token) 

        config.headers.Authorization = 'Bearer ' + $localStorage.token; 
        console.log(config.headers); 
       } 
       return config; 
      }, 
      'responseError': function(response) { 
       if(response.status === 401 || response.status === 403) { 
        $location.path('#login'); 
       } 
       return $q.reject(response); 
      } 
     }; 
}]); 

這裏是我的HTTP請求

$scope.getSearcHistory = function(){ 
    console.log($localStorage.token); 
    console.log($rootScope.getCurrentUser()); 
    $http({ 
     url: baseUrl+'userSearches/2', 
     method : 'GET', 
     headers: { 
      'key': '1234424252' 
     } 
    }) 
    .success(function(data, status, headers, config){ 
     console.log(data); 
     $scope.searches = data; 
    }) 
    .error(function(data, status, headers, config){ 
     console.log(status); 
    }); 
} 

這是我的位指示的NodeJS

this.router.use(function(req, res, next){ 
     console.log(req.headers); 
     var bearerToken; 
     var bearerHeader = req.headers.authorization; 
     console.log(bearerHeader) 
     if (typeof bearerHeader !== 'undefined') { 
      var bearer = bearerHeader.split(" "); 
      bearerToken = bearer[1]; 
      req.token = bearerToken; 
      next(); 
     } else { 
      res.send(403).json({"Authorization": "Not allowed"}); 
     } 
    }); 

這裏是我的節點控制檯,看有沒有頭,我需要 -

{ 
host: 'localhost:8080', 
connection: 'keep-alive', 
'access-control-request-method': 'GET', 
origin: 'http://localhost:440', 
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 
'access-control-request-headers': 'authorization,key', 
accept: '*/*', 
referer: 'http://localhost:440/vokal/'} 

回答

0

根據你的代碼,似乎你不應該有任何問題。你在做什麼的角度最好應該給你的自定義頭

$http({ 
    method: 'GET', 
    url: '/api', 
    headers: { 
     'Authorization': 'test', 
     'Accept': 'application/json', 
     "X-Test-Header": 'true' 
    } 
}).then(function(response) { 
    if (response.data == 'ok') { 
     // success 
    } else { 
     // failed 
    } 
}); 

請問您檢查您的控制檯一次是否你所得到的頭。

如果連這個不工作,那麼也許你需要設置節點,以允許所有頭

var allowCrossDomain = function(req, res, next) { 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); 
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); 
}; 

,並在您的應用程序

app.configure(function() { 
    app.use(app.router); 
    app.use(allowCrossDomain); 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

配置它所有這一切都只是處理與否自定義標題可以發送或不發送。如果您使用的是授權令牌,則需要在$ http中以角度正確地設置它,以便每次發出請求時都可以發送它。爲此,你可能需要包括

$http.defaults.headers.common['x-access-token'] = jwt 

希望這會有所幫助。

+0

問題是,當我在Express應用程序中獲取標題時,請檢查我的問題'access-control-request-headers:'authorization,key''但我的req.headers.authorization和req.headers.key都未定義。我的意思是如何訪問該值? – Shailendra2014

+0

如果你在req.headers上打印,你會得到什麼樣的價值? –

+0

我的問題中的最後一個代碼塊是'console.log(req.headers') – Shailendra2014