我正在fetch()瀏覽器中使用 Rest 和API 來發布帶有x-api-key標題的資料以進行授權。我用 Postman 測驗了 API,curl它作業得很好:
curl --header "X-API-KEY: my-api-key" -d "my-request-body" -X POST http://localhost:8081/submit
但是,它不適用于 fetch api。在 Chrome 上:
const response = await fetch(url, {
method: 'POST',
mode: 'no-cors',
cache: 'no-cache',
headers: {
'x-api-key': 'my-api-key',
'Accept': 'text/plain',
'Content-Type': 'text/plain'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: 'my-request-body'
});
我無法從請求中檢索 API 密鑰。在我的快遞中,路由器處理程式:
router.post('/submit', cors(), async (req,res, next) => {
const apiKey = req.header('x-api-key');
console.log(apiKey) // THIS GIVES undefined
});
沒有x-api-key報頭存在于req.headers物件要么。請幫忙
uj5u.com熱心網友回復:
我看到一個問題,不確定這是否是導致它的原因,但您忘記,了您的 fetch 標頭,也許在添加,?
uj5u.com熱心網友回復:
行。在與它斗爭了一段時間后,我發現了導致它的問題......mode: 'no-cors'一直在導致它。mode: 'cors'出于某種原因,只需將其設定為,您就可以開始了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/387184.html
