我正在制作一個 MERN 應用程式。在 useEffect 中,我正在進行 API 呼叫,在其中設定了一個 autorizarion 令牌。但是我無法從后端的標頭中獲取授權令牌。
useEffect(()=>{
fetch(`${API}/${userId}/courses`,{
method:"GET",
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}).then(response=>{
return response.json();
}).then(data=>{
console.log(data)
})
})
如果我嘗試req.headers在后端使用 console.log ,我會收到如下回應:
{ host: 'localhost:3030',
connection: 'keep-alive'
origin: 'http://localhost:3000',
referer: 'http://localhost:3000/',
}
其中沒有授權屬性。但是,如果我嘗試從 POSTMAN 進行相同的 api 呼叫,則會在標頭中獲得授權令牌。有什么問題?
uj5u.com熱心網友回復:
您必須包裝標頭以鍵入headers引數物件。
fetch('${API}/${userId}/courses',{
method:"GET",
headers: {
'Authorization': 'Bearer ${token}',
'Content-Type': 'application/json'
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/336046.html
標籤:javascript 反应 表达
上一篇:Spark的spark_driver_dagscheduler_messageprocessingtime_type_timers_count指標的增加行為是為了什么?
