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就是用來設置狀態碼的。