使用 axios 發送 POST 請求時收到禁止請求。
這是我的 axios 函式:
const headers = {
'Authorization': `bearer ${JSON.parse(localStorage.getItem('TOKEN'))}`,
'Content-Type': 'application/json'
}
const getByRelationCode = (data) => {
const response = Client.post(`${TRACKING_API}/getByRelationCode`, data, {
headers
})
return response
}
這是呼叫函式
const getDocument = (relationCode) => {
await Service.getByRelationCode({ relationCode })
.then((res) => {console.log(res))
.catch(err => console.log(err))
}
當我使用郵遞員點擊端點時,端點回傳了回應。但是當我使用 Axios 呼叫它時,它會回傳一個禁止的請求。我的代碼中是否缺少?
uj5u.com熱心網友回復:
試試這樣
const config = {
headers: {
'Authorization': `bearer ${localStorage.getItem('TOKEN')}`,
'Content-Type': 'application/json' }
};
const getByRelationCode = (data) => {
const response = Client.post(`${TRACKING_API}/getByRelationCode`, data, config)
return response;
}
uj5u.com熱心網友回復:
我認為這是因為您Bearer在代碼中拼寫錯誤并且服務器收到您的請求但無法從中提取令牌。
嘗試使用正確的拼寫Bearer。
const headers = {
'Authorization': `Bearer ${JSON.parse(localStorage.getItem('TOKEN'))}`,
'Content-Type': 'application/json'
}
uj5u.com熱心網友回復:
您的標題拼寫錯誤。
const headers = {
'Authorization': `Bearer ${JSON.parse(localStorage.getItem('TOKEN'))}`,
'Content-Type': 'application/json'
}
但是在某些框架中我們可以自定義它。如果您為自己定制了它,那么您可以將其稱為bearer. 雖然這樣命名是不正確的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/476199.html
標籤:javascript 节点.js 反应 axios
上一篇:輸入欄位“占位符”不作業/顯示
