我正在制作一个MERN应用程序。在useEffect内部,我进行了一个API调用,并在其中设置了一个autorizarion令牌。但是我无法从后台的头部获取授权token。
useEffect(()=>{
fetch(`${API}/${userId}/courses`,{
method:"GET",
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}).then(response=>{
return response.json();
}).then(data=>{
console.log(data)
})
})
如果我在后端尝试console.log req.headers
,得到的响应如下:
{ host: 'localhost:3030',
connection: 'keep-alive'
origin: 'http://localhost:3000',
referer: 'http://localhost:3000/',
}
其中没有authorization属性。但是,如果我尝试从POSTMAN发出相同的api调用,我会在报头中获得授权令牌。有什么问题吗?
发布于 2021-10-25 12:06:13
你必须在arguments对象中包装headers
的头部。
fetch('${API}/${userId}/courses',{
method:"GET",
headers: {
'Authorization': 'Bearer ${token}',
'Content-Type': 'application/json'
}
https://stackoverflow.com/questions/69707103
复制相似问题