axios与springboot后端的数据传递

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就是用来设置状态码的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章