我正在用 Flask 和 React 制作一个网站。用户在表单上输入电子邮件和密码,axios.post到后端,后端检查电子邮件和密码与数据库是否正确,如果正确,后端返回一个cookie。 cookie 是在邮递员中设置的,而不是在我的浏览器中设置的。显然,前端在我的浏览器中没有从后端接收任何内容,因为它甚至没有打印我放入 axios.post 中的 console.log。在邮递员中设置了cookie,而不是在我的浏览器中
axios 帖子:
const handleLogin = function(){
axios.post("http://127.0.0.1:5000/login", {email:email, password:password}, {withCredentials:true, headers : {'Content-Type' : 'multipart/form-data'}}).then(response => {
if(response.data["message"] == "Logado com sucesso"){
history.push('/');
}
}).catch(error => {alert(error.response.data); console.error("Error occurred in login request:", error);});
烧瓶返回:
if(check_password_hash(data[19],password)):
resposta = make_response(jsonify({"message": "Logado com sucesso"}))
resposta.set_cookie('id', str(data[0]), httponly=True, samesite='None')
return resposta
显然前端在我的浏览器中没有从后端接收到任何内容,因为它甚至没有打印我放入 axios.post 中的 console.log。我将 CORS 设置为支持凭据和 axios.post withCredentials:true。
每次前端执行请求函数时,都会刷新页面,从而阻止其设置任何cookie。 所以我只是将前端更改为.