授權標頭(不記名令牌)未添加到 api 呼叫中
狀態為 401 未自動處理且標頭未添加到 api 呼叫中。
const axios = require('axios')
let token = 'eyJ0eXAiOiJOiJIUzI1NiJ9.eyJpc3MiOiJJQ00iLCJhdWQiOiJzZXNzaW9uLW1hbm'
export class classname )
async getReports ()
{
let response
try {
response = await axios.get(`https://urltogo/path`), {
headers: {
'Content-Type' : 'application/json',
Authorization : `Bearer ${token}`
}
}
const responseObj = {
url: `GET ${`https://urltogo/path`}`,
status: response.status,
data: response.data
}
if (responseObj.data.meta.count == 1) {
return responseObj.data.items[0].id
}
} catch (error) {
const errorObj = {
status: error.response?.status,
data: error.response?.data
}
throw new Error(JSON.stringify(errorObj))
}
}
}
獲取錯誤
"status":401,"data":{"message":"Unauthorized, **no authorization header value**"}}
資料:error.response?.data
not sure what i am missing here in the code
uj5u.com熱心網友回復:
您需要將選項作為get方法的第二個引數,而不是在關閉它之后。
response = await axios.get(`https://urltogo/path`, {
headers: {
'Content-Type' : 'application/json',
Authorization : `Bearer ${token}`
}
});
uj5u.com熱心網友回復:
用粗體更新了@reyno 的答案
response = await axios.get**(**`https://urltogo/path`,{
headers: {
'Content-Type' : 'application/json',
'Authorization' : `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ`}
} **)**;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/369893.html
標籤:javascript 节点.js 接口 公理 玩笑
