注销时,会发生此错误。从源“ http://localhost:3000 ”访问“ http://127.0.0.1:8000/api/auth/logout/ ”的 XMLHttpRequest已被 CORS 策略阻止:访问不允许请求标头字段身份验证-预检响应中的 Control-Allow-Headers。
我在 onClick 中调用 auth.js 的注销功能。我不知道为什么刷新页面时会注销。
减速器.js
case LOGOUT_SUCCESS:
localStorage.removeItem("token");
return {
...state,
isAuthenticated: null,
isLoading: false,
user: null,
isFreelancer: false
};
action/auth.js 的注销功能
export const logout = () => (dispatch, getState) => {
const token = getState().auth.token;
const config = {
headers: {
"Content-Type": "application/json"
}
};
if (token) {
config.headers["Authentication"] = `Token ${token}`;
}
axios
.post("http://127.0.0.1:8000/api/auth/logout/", null, config)
.then(res => {
dispatch({ type: LOGOUT_SUCCESS });
})
.catch(err => {
console.log(err.message);
});
};
我希望立即注销,但每次刷新页面时都会注销。