将 jwt 令牌添加到 GET 请求在 React 中不起作用
Adding jwt token to GET request not working in React
为了从 API 获取数据,我需要在调用的 header 中发送一个 jwt 令牌。在我的应用程序中获取它后,我将它保存在本地存储中,当我检查浏览器的控制台时,我可以看到保存它有效 - 当我执行 localStorage.getItem('token') 那里。但是当我尝试将它添加到我的 API 调用中时,我收到错误消息 no jwt found。显然,在执行从本地存储获取令牌的逻辑之前,正在执行 get 请求。下面是我的代码。任何有关如何解决此问题的建议将不胜感激。谢谢 !
const url = 'https://url.com';
const token = localStorage.getItem('token');
const AuthStr = 'Bearer '.concat(token);
export default class TestCall extends React.Component {
constructor(props) {
super(props);
this.state = {
error: undefined,
isLoaded: false,
items: [],
};
this.getData = this.getData.bind(this);
}
componentDidMount() {
this.getData();
}
getData () {
return fetch(url, {
method: 'GET',
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
Authorization: "Bearer " + AuthStr,
})
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
const { error, isLoaded, items } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<p>{items} </p>
);
}
}
}
更新:
根据我收到的答复,我将顶部的代码更改为:
const token = localStorage.getItem('token');
我删除了 const AuthStr。
所以我现在的获取请求是:
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
Authorization: "Bearer " + token,
})
这应该已经修复了拼写错误,但我得到的回复仍然没有找到 jwt。
似乎在初始化 const AuthStr
时,您已经向其中添加了 Bearer
字符串:
const AuthStr = 'Bearer '.concat(token);
.
然后,在设置请求时再次执行此操作:
Authorization: "Bearer " + AuthStr
.
所以你实际发送的内容看起来像这样:Bearer Bearer your token
.
您是否尝试在 getData()
函数或 componentDidMount()
中实例化 AuthStr
参数?
感谢@Yoav 帮助我实现它!
但是,问题似乎出在其他地方 - 我没有在提取请求中的正确位置添加令牌。
有效的代码是这样的:
getData(){
let auth = localStorage.getItem('user_token');
return fetch(url, {
method: 'GET',
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': "Bearer " + auth,
},
})
.then(res => res.json())
我什至可以在执行 fetch 之前定义 auth 变量并在函数中获取令牌 - 在 componentdidmount 中调用它也有效,但这不是主要问题。
对于在您将令牌放入请求中的此手表上工作的任何其他人!
为了从 API 获取数据,我需要在调用的 header 中发送一个 jwt 令牌。在我的应用程序中获取它后,我将它保存在本地存储中,当我检查浏览器的控制台时,我可以看到保存它有效 - 当我执行 localStorage.getItem('token') 那里。但是当我尝试将它添加到我的 API 调用中时,我收到错误消息 no jwt found。显然,在执行从本地存储获取令牌的逻辑之前,正在执行 get 请求。下面是我的代码。任何有关如何解决此问题的建议将不胜感激。谢谢 !
const url = 'https://url.com';
const token = localStorage.getItem('token');
const AuthStr = 'Bearer '.concat(token);
export default class TestCall extends React.Component {
constructor(props) {
super(props);
this.state = {
error: undefined,
isLoaded: false,
items: [],
};
this.getData = this.getData.bind(this);
}
componentDidMount() {
this.getData();
}
getData () {
return fetch(url, {
method: 'GET',
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
Authorization: "Bearer " + AuthStr,
})
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
const { error, isLoaded, items } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<p>{items} </p>
);
}
}
}
更新:
根据我收到的答复,我将顶部的代码更改为:
const token = localStorage.getItem('token');
我删除了 const AuthStr。 所以我现在的获取请求是:
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
},
Authorization: "Bearer " + token,
})
这应该已经修复了拼写错误,但我得到的回复仍然没有找到 jwt。
似乎在初始化 const AuthStr
时,您已经向其中添加了 Bearer
字符串:
const AuthStr = 'Bearer '.concat(token);
.
然后,在设置请求时再次执行此操作:
Authorization: "Bearer " + AuthStr
.
所以你实际发送的内容看起来像这样:Bearer Bearer your token
.
您是否尝试在 getData()
函数或 componentDidMount()
中实例化 AuthStr
参数?
感谢@Yoav 帮助我实现它!
但是,问题似乎出在其他地方 - 我没有在提取请求中的正确位置添加令牌。
有效的代码是这样的:
getData(){
let auth = localStorage.getItem('user_token');
return fetch(url, {
method: 'GET',
headers:{
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': "Bearer " + auth,
},
})
.then(res => res.json())
我什至可以在执行 fetch 之前定义 auth 变量并在函数中获取令牌 - 在 componentdidmount 中调用它也有效,但这不是主要问题。
对于在您将令牌放入请求中的此手表上工作的任何其他人!