Joey0610 2022-05-09 17:36 采纳率: 50%
浏览 35
已结题

为什么账号密码在测试中是对的返回200了,但是浏览器上返回401呢

login(userForm) {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          console.log("userform");
          console.log(this.userForm);
          // 表单校验合法
          // 这是参数一和参数二
          // this.$axios.post("/api/backend/login", this.userForm)
          // 这种你需要指定method
          // this.$axios({
          //   method: 'post',
          //   url: '/api/backend/login',
          //   data: this.userForm
          // })
          // 这种是错的
          // axios.post(url[, data[, config]])
          this.$axios
            .post("/api/backend/login", 
              // data: JSON.stringify(this.userForm),
              this.userForm,
              // contentType: 'application/json;charset=UTF-8'
            )
            .then((res) => {
              // console.log(username + password);
              console.log("5");
              console.log(res);
              if (res.code === "200") {
                // localStorage.setItem("user", JSON.stringify(res.data)); // 存储用户信息到浏览器
                // localStorage.setItem("menus", JSON.stringify(res.data.menus)); // 存储用户信息到浏览器
                // 动态设置当前用户的路由
                // setRoutes();
                console.log("登录成功");
                // this.$message.success("登录成功");
                // localStorage.setItem("user_id", id);
                // if (res.data.role === "ROLE_STUDENT") {
                //   this.$router.push("/PageTable");
                // } else {
                //   this.$router.push("/");
                // }
              } else if (res.code === "1005") {
                console.log("6");
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              console.log(err);
              console.log("登陆失败");
              if (err.code === "1005") {
                console.log("6");
              }
            });
          console.log("7");
        }
      });
    },
  },

img

img

img

  • 写回答

1条回答 默认 最新

  • go_with_dream 2022-05-09 19:05
    关注

    可以看一下你测试时发的url以及header和你在浏览器时network里边的的url和header吗,比较一下,看看是不是url或header有不同

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月1日
  • 已采纳回答 5月24日
  • 创建了问题 5月9日