解决post请求httpServletRequest对象.getParameter获取不到参数!

httpServletRequest对象.getParameter()接收post请求参数,发送端post的config参数里的content Type必须设置为application/x-www-form-urlencoded;否则会接收不到

代码示例如下:

          var url = `http://localhost:8080/javaWeb/Login`;
          var data = {
            username: this.userForm.acount,
            password: this.userForm.pass,
          };
          var config = {
            headers: {
              //配置请求表头防止后端接收不到data中的参数
              "Content-Type": "application/x-www-form-urlencoded",
              // 可以在这里添加其他的header配置
            },
          };
            //使用axios发送post请求
            this.axios
            .post(url, data,config)
            .then((res) => {
              console.log(res);
              //使用res.data.data 获取自己封装的对象中的数据
              console.log("data", res.data.data);
              if (res.data.status == 200) {
                //将响应数据缓存到本地
                localStorage.setItem("user", res.data.data);
                //登陆成功跳转到主页
                this.$router.push("/index");
                //登陆成功提示
                this.$message({
                  message: res.data.msg,
                  type: "success",
                });
              }
            })
            .catch((rej) => {
              //提示错误信息
              this.$message({
                message: "用户名或者密码错误",
                type: "error",
              });
              //请求失败捕获
              console.log(rej);
            });
        } else {
          console.log("error submit!!");
          return false;
        }

猜你喜欢

转载自blog.csdn.net/qq_58647634/article/details/131960710
今日推荐