axios
let data = {username:this.ruleForm.username,password:this.ruleForm.password};
this.axios.post("http://localhost:8181/user/login",data)
.then((response) =>{
if(response.code === 200){
alert(response.data);
}
}).catch((error)=>{
console.log(error)
});
springboot controller内
@Controller
@RequestMapping("/user")
public class UserController {
// Post请求
@RequestMapping(value="/login",method = RequestMethod.POST)
public ResponseEntity<Map<String,Object>> login( @RequestBody Map map){
System.out.println(map.get("username"));
return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
}
}
其中,@RequestBody注释可以获取json数据(请求体中的数据),此处直接用map来接收json参数。返回的时候需要设置状态码,否则会出现后台数据请求成功但是前端返回404的错误。ResponseEntity就是用来设置状态码的。