【发布时间】:2021-11-09 00:26:42
【问题描述】:
我正在使用 Axios 向服务器发送请求。这是我使用的实例:
export default axios.create({
baseURL: "https://wagal.com",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Authorization": `Bearer ${store.getState().auth.userData.token}`,
},
});
这是我如何使用上述实例来启用和禁用 2fa:
function switchTfa(data) {
if (data) {
return (dispatch) => {
return api_request
.post("/api/user/tfa/enable", {})
.then((response) => {
dispatch(isLoading(false));
dispatch(
userData({
tfa_enabled: data,
user_token: store.getState().auth.userData.token,
})
);
console.log("Success");
})
.catch((error) => {
dispatch(isLoading(false));
console.log(`error.message`, error.message);
});
};
} else {
return (dispatch) => {
dispatch(isLoading(true));
return api_request
.post("/web-api/user/tfa/disable", {})
.then((response) => {
dispatch(isLoading(false));
dispatch(
userData({
tfa_enabled: data,
user_token: store.getState().auth.userData.token,
})
);
console.log("Success");
})
.catch((error) => {
dispatch(isLoading(false));
console.log(`error.message`, error.message);
});
};
}
}
现在,如果我发送此请求,我会收到以下错误:
Request failed with status code 400
如果我在 Axios 实例中取消引用“授权”,它会起作用
export default axios.create({
baseURL: "https://wagal.com",
headers: {
"Content-Type": "application/json; charset=utf-8",
Authorization: `Bearer ${store.getState().auth.userData.token}`,
},
});
现在,如果我重新加载应用程序并发送请求,它会抛出这个 400 错误,但如果我再次用引号包裹“授权”,它会发送请求,奇怪!!!
为什么每次重新加载后我有时会通过引用和有时通过取消引用“授权”得到 400 个错误?
谢谢
【问题讨论】:
-
当然引号或不直接与任何行为变化无关;它们只是触发热重载或刷新,进而导致您看到的行为。
标签: javascript reactjs react-native axios