我需要訪問服務器端(Node)中的axios標頭授權令牌,顯示未定義。請幫助..節點服務器無法檢索反應axios請求標頭參數的值
客戶端(反應)要求:
var config = {
headers: {
'cache-control':'no-cache',
'content-type': 'application/x-www-form-urlencoded',
'authorization' :'bearer '+Auth.getToken()
}
};
axios.get(ApiConfig.API_BASE+'api/admin/profile/', config).then(function(response) {
this.setState({status:'success', profile: response.data.data});
}).catch(function(response) {
console.log(response);
});
服務器端(節點):
module.exports = (req, res, next) => {
console.log(req.headers.authorization);
if(!req.headers.authorization) {
return res.status(401).end();
}
};
日誌顯示不明確的。我還安慰了整個頭部,但它們的輸出是:
{ host: 'localhost:8027',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate',
'access-control-request-method': 'GET',
'access-control-request-headers': 'authorization,cache-control',
origin: 'http://localhost:3001',
connection: 'keep-alive' }
如何獲取授權令牌值?
謝謝。
[如何從請求中使用NodeJS連接提取請求http頭]可能的重複(https://stackoverflow.com/questions/13147693/how-to-extract-request-http-headers-from-a-request -using-nodejs-connect) – mersocarlin