今天做Vue和SpringBoot数据交互项目遇到一个bug
在这里插入图片描述

像个鬼一样一直报错

后来仔细查了很多资料

原来是参数错啦

在这里插入图片描述

看看前端代码

var Main = {
	data() {

		return {
			ruleForm: {
				name: '',
				password: ''
			},
			rules: {
				name: [
					{ required: true, message: '请输入用户名称',trigger:'blur' }
				],
				password: [
					{ required: true, message: '请输入密码',trigger:'blur'  }
				]
			}
		};
	},
	methods: {
		submitForm(ruleForm) {
			this.$refs[ruleForm].validate(async (valid) => {
				if (valid) {

					axios.post('/admin/login',this.ruleForm).then((res) =>{
						console.log(res)
						console.log(res.config.data)
						res = JSON.stringify(res.config.data)
						console.log(res)
					})

					alert('submit!');
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		resetForm(ruleForm) {
			this.$refs[ruleForm].resetFields();
		}
	}

}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#textInput')

接口代码

@PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession session, RedirectAttributes redirectAttributes) {
        User user = userService.checkUser(username,password);

        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        }else {
            redirectAttributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }

    }

前端传的是对象而后端接受的字符串,所以报参数错误

修改后端代码

@PostMapping("/login")
    public String login(@RequestBody User user, HttpSession session, RedirectAttributes redirectAttributes) {
        User user = userService.checkUser(username,password);

        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        }else {
            redirectAttributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }

    }

ok

Logo

前往低代码交流专区

更多推荐