会话不存储在POST请求中,而是存储在Get请求中

发布于 2025-01-22 01:55:20 字数 1192 浏览 0 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-01-29 01:55:20

打开Chrome的开发人员工具,以检查烧瓶响应中的set-cookie标题。

如果烧瓶工作正常,那么问题可能会在前端侧发生。

Try this:

axios.post('your_url', data, {withCredentials: true})

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:

axios.post('your_url', data, {withCredentials: true})

This may help you too: Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文