Front-End/ajax

ajax encodeURIComponent로 파라미터 넘길시 한글 깨짐현상... (2)

ducks228 2010. 5. 26. 15:18
밑의 글을 바탕으로 내 나름 해결을 해 봤다.

System.out.println(System.getProperty("file.encoding"));

먼저 내 톰캣의 캐릭터 셋을 알아봤다.. 결과

--> MS949

파라미터를 넘겨주는 펑션에서

content = encodeURIComponent(content);
 name =encodeURIComponent(name);
 var params = "name= "+encodeURIComponent(name)+"&content="+encodeURIComponent(content)+"&board_idx="+encodeURIComponent(board_idx);

두번 인코딩을 해줬다..
한번만 했을 시에 자바로 넘어갈때 깨져서 들어간다...
밑에 글과 원리는 같다.

//ajax에서 한글을 encodeURIEncoding으로 변환해서 값을 가져오면
 //웹서버가 MS949캐릭터 셋일때는 한글이 깨져서 나온다..
 //서버전체를 UTF-8캐릭터셋으로 바꿔주는수도 있지만 그렇게 하지 않고 이 메서드를 이용해서
 //변환을 해준다.
 public static String deCode(String val) throws UnsupportedEncodingException{
 
  String a = val;
  System.out.println("originA= "+a);
 
  a = URLDecoder.decode(a, "UTF-8");
  System.out.println("a0= "+a);
 
  a = URLDecoder.decode(a, "MS949");
  System.out.println("a2= "+a);
   
  return a;
 }

자바메소드에서 이런식으로 넘어온 값을 디코딩해주면 문제 없이 한글이 표현되더라..