首页 > 解决方案 > vue中的相同功能反应错误

问题描述

我在 Vue(工作)中有一个 JWT 身份验证,我试图更改为 React(它的后端相同)。在 console.log(this.state.email) 中,我收到了正确的电子邮件,与密码相同。我认为问题出在 'try{const response = await axios.post("api/auth/login"...' 我不能仅在 Vue 中通过 React

反应错误:

  EnvioLogin = async () => {
    console.log(this.state.email)
    console.log(this.state.password)
    try {
      const response = await axios.post("api/auth/login", { email: this.state.email, password: this.state.password });
      localStorage.setItem("token", response.data.token);
      if (response.status === "200") {
        this.getUsers();
        console.log(response);
        this.props.history.push("/app");
      }
    } catch (error) {
      this.setState({ error: "User or Password wrong!" });
      setTimeout(() => {
        this.setState({ error: "" });
      }, 2000);
    }
  };

Vue 正确:

async EnvioLogin() {
  try {
    console.log(this.email);
    console.log(this.password);
    const response = await axios.post("api/auth/login", {
      email: this.email,
      password: this.password,
      
    });
            localStorage.setItem("token", response.data.token);
    if (response.status == "200") {
      this.getUsers();
      console.log(response);
      this.$router.push("intermediorotas");
      this.showLogin = false;
    }
  } catch (error) {
    this.showError = true;
    setTimeout(() => {
      this.showError = false;
    }, 2000);
  }
},

标签: reactjsvue.js

解决方案


推荐阅读