[英]Why axios remove "Bearer" string from authorization header?
我在使用不記名令牌時遇到了 axios 和 GET 請求的問題。 這是我的代碼:
const response = await axios.post("/auth/login", {
...form
});
const token = response["token"];
if (token) {
localStorage.setItem("session", token);
console.log(token); //a234o2i3nr203fn20jef0293...
const tokenHeader = {
authorization: `Bearer ${token}`
}
const projects = await axios.get('/projects', { headers: { "authorization": `Bearer ${token}`}});
console.log(projects); //UNAUTHORIZED
}
登錄帳戶后,我想立即獲取有關項目的數據,因此我使用 axios 並嘗試使用axios.get
授權 header。 但是,由於我未知的原因,我的服務器部分請求已交付但在我的token
之前沒有Bearer
字符串,我的服務器在此請求上有這個:
headers: {
authorization: "a234o2i3nr203fn20jef0293..." //axios remove `Bearer` string?
}
因此,我無法返回任何值,因為我的服務器未經授權返回給我,原因如下:
const token = authHeaders && authHeaders.split(" ")[1];
返回我 null 而不是令牌。
有人能告訴我這個 axios 請求有什么問題嗎?
謝謝你的幫助!
嘗試寫“授權”而不是“授權”。 我以前也遇到過這樣的問題。
const projects = await axios.get('/projects', { headers: { "Authorization": `Bearer ${token}`}});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.