会话不存储在POST请求中,而是存储在Get请求中
我正在python烧瓶上的react.js和服务器中进行前端,并将mongoDB用作数据库。当我在登录的React JS上输入凭据时,我会使用Axios将数据发送到服务器。 Python检查凭据,将用户数据存储在数据库上并创建用户。我使用POST请求在Python服务器上创建会话。问题在于,当我使用POST请求将数据发送到服务器时,即使我为此编写了代码,也不会进行会话。 这是后端代码。
@app.route('/login', methods=['POST', "GET"])
def LoginScreen():
if request.method == "POST":
data = request.json
email = data['email']
password = data['password']
user = db.Users.find_one({"email": email, "password": password})
print(user['_id'])
if user is None:
return {"status": "fail"}
else:
session['user'] = {"userID": str(user['_id'])}
print(session['user'])
return {"status": "success"}
else:
session['user'] = "AbdulMunim"
return 'LoginPage'
是前端代码,它不会存储会话,但是当使用服务器端的局部主机并使用获取的登录方法时,它会存储会话。
await axios
.post("http://localhost:5000/login", {
email: email,
password: password,
})
.then((res) => {
console.log("RES", res);
if (res.data.status === "success") {
navigate("/");
} else {
alert("Invalid Credentials");
}
});
这是当我使用Post方法将数据发送到烧瓶中时,这
I'm making front end in React.js and server on python flask and using mongodb as database. When I enter credentials on react js for logging in, I send data to server using axios. python checks the credentials, stores the user data on database and creates a user. I create a session on python server using post request. The problem is this that when I send the data to server using post request, it doesn't make a session even though i wrote the code for that.
This is the backend code.
@app.route('/login', methods=['POST', "GET"])
def LoginScreen():
if request.method == "POST":
data = request.json
email = data['email']
password = data['password']
user = db.Users.find_one({"email": email, "password": password})
print(user['_id'])
if user is None:
return {"status": "fail"}
else:
session['user'] = {"userID": str(user['_id'])}
print(session['user'])
return {"status": "success"}
else:
session['user'] = "AbdulMunim"
return 'LoginPage'
and this is the front end code
await axios
.post("http://localhost:5000/login", {
email: email,
password: password,
})
.then((res) => {
console.log("RES", res);
if (res.data.status === "success") {
navigate("/");
} else {
alert("Invalid Credentials");
}
});
When I use POST method to send data from react to flask, it doesn't stores the session but when use the localhost of server side and use login method which is GET, it stores the session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
打开Chrome的开发人员工具,以检查烧瓶响应中的
set-cookie
标题。如果烧瓶工作正常,那么问题可能会在前端侧发生。
Try this:
This may help you too:
Open chrome's developer tool to check if there's a
set-cookie
header in flask's response.If flask works fine, then the problem may happen on the frontend side.
Try this:
This may help you too: Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?