2013년 7월 9일 화요일

ajax 인코딩 관련

기본적으로 ajax 는 utf-8 로 통신하게 되어있기 때문에 한글을 보내거나 받을 경우
그에 맞는 인코딩 설정을 해주어야 한다.
단, json 객체로 보내게 되면 굳이 인코딩을 할필요는 없다.
json 객체를 전송할때는

contentType : "application/json",

json 객체를 받을때는

dataType : "json",

을 명시해주면 된다. 하지만 일반 text일 경우에는
추가로 설정이 필요하다.

전송할때

contentType:"application/x-www-form-urlencoded; charset=UTF-8",

추가해주고 받을때는 

별도의 설정은 필요없다.

컨트롤러단에서 @ResponseBody 사용시
@RequestMapping(value = "/test/encoding5", method = RequestMethod.GET, produces="text/plain; charset=UTF-8")
설정해주고 @ResponseBody 미사용시

response.setCharacterEncoding("utf-8");

이 구문을 추가해준다.

댓글 없음:

댓글 쓰기