WebDev/Spring

@ResponseBody을 통한 ajax통신 결과값 받기

ajax와 스프링 통신을할때 


일반적인 @RequestMapping으로 통신하면 원하는 결과값을 얻어올 수 없다.


이럴때 @ResponseBody를 사용하면 값을 ajax와 통신할 수있다.


$.ajax({ type:"POST", url:"/error-404.tistory.do", data:"data자료들", async: false, success:function(result){ alert(result) },error:function(e){ alert("실패하였습니다") }



이렇게 이런 일반적인 ajax문에서



@RequestMapping(value = "/error-404.tistory.do", method=RequestMethod.POST,produces="text/plain;charset=UTF-8")
@ResponseBody
public String error-404tistory(ModelMap model) throws Exception {
		return "결과를알려줘!";
}



와 같이 수정을하면


alert에 결과를 알려줘라는 결과값을 얻을 수 있다.