axios发送post请求到后端controller接受参数为null
// 登录Controller
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public String login(@Param("account") String account, @Param("password") String password) {
System.out.println(account + " " + password);
User byAccount = service.getUser(account, password);
System.out.println(byAccount);
if (byAccount != null) {
System.out.println("登录成功");
return "true";
} else {
System.out.println(account + " " + password);
System.out.println("登录失败");
return "false";
}
}
<body>
<div id="login" class="login">
<form id="l" action="/login" method="post">
<h1>登录测试</h1>
<input id="account" type="text">账号<br>
<input id="password" type="password">密码<br>
<input type="button" id="btn" value="提交">
</form>
</div>
</body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
document.getElementById("btn").onclick = function () {
var formData = {
account: "",
password: ""
}
let account = document.getElementById("account").value;
formData.account = account;
let password = document.getElementById("password").value;
formData.password = password;
console.log(formData);
axios({
method: 'post',
url: 'http://localhost:80/login',
data: formData
}).then(function (resp) {
console.log(resp)
if (resp.data == "true") {
window.alert("登录成功")
console.log("ss")
} else {
console.log("ssas")
window.alert("登录失败")
}
});
}
</script>
搜索同样问题在网上使用URLSearchParams但是没有用
后端打印的出这两个值为null,使用postMan访问后端login正常无错误,但是在html表单提交数据一直都是登录失败
希望能够指出语法错误或者多余字段指明