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

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