메뉴 건너뛰기

?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

자바스크립트에서 비동기로 ajax로 컨트롤러로 요청을 받아 db에서 값을 꺼내 문자열을 리턴할 경우, 한글 문자열을 리턴했을 때

ajax의 success:function(result) 안에서 result로 값을 받으면 ???? 로 한글이 깨지는 경우가 있다.

이럴 경우에는 컨트롤러에서 produces 부분을 지정해주면 된다.


코드를 보면


먼저, ajax 통신 부분이다. /board/category/getAddr로 요청을 보내고 


   var address = null;

function getAddr(){

    $.ajax({

          type:'post',

          headers:{

              "Content-Type":"application/json"

          },

          async : false, // ajax를 동기화(순서대로) 처리해야하는 경우 true로하거나 기술하지 않으면 비동기로 작동한다.

          url:"/board/category/getAddr?userId=${boardDTO.userId}",

          dataType:"text",

          success : function(result){

              if ( result != null ){

                  console.log("넘어온 값 : " + result);

                  address = result;   

              }

          }

      });

};

컨트롤러 부분이다.
컨트롤러에서 service.getAddr(userId)로 해당 사용자의 주소를 얻어온 뒤 그 주소를 반환하지만 한글로 반환하게 된다.
이 경우, ajax의 success:function(result) 에서 result 가 ????로 깨지는 것을 알 수 있다.

하지만, produces = "application/text;charset=utf8"을 지정해 줄 경우 한글을 인코딩해 보내서 깨지지 않고 처리할 수 있게 된다.



// 한글을 넘기기 때문에 produces 를 기술해서 인코딩을 해서 넘겨주었다. 기술안하면 ajax에서 받았을 때 ???로 깨짐!

    @RequestMapping(value="/board/category/getAddr",method=RequestMethod.POST,produces = "application/text; charset=utf8")

    public @ResponseBody String getAddr(String userId){

        logger.info("주소얻기로 넘어온 아이디 : " + userId);

        String address = service.getAddr(userId);

        logger.info("얻어온 주소 : " + address);

        return address;

    }



  1. AJAX 통신시 컨트롤러에서 한글 문자열을 리턴해야하는 경우 인코딩 문제 처리

    Date2018.07.04 Views2599
    Read More
  2. JAVA Crawling(크로울링) 기본([펌]http://partnerjun.tistory.com/guestbook)

    Date2018.07.04 Views5112
    Read More
  3. Java was started but returned exit code=13 - 이클립스 실행시 에러

    Date2021.03.31 Views152
    Read More
  4. Nutch 0.9 를 이용하여 한글 검색하기

    Date2016.12.08 Views5934
    Read More
  5. play framework + 이클립스 초간단 개발환경 구축법

    Date2016.12.08 Views12468
    Read More
  6. solr적용시 사용법

    Date2016.12.08 Views5582
    Read More
  7. Spring AOP(Aspect Oriented Programming) 에서의 용어

    Date2016.12.08 Views5176
    Read More
  8. Spring Framework 개요

    Date2016.12.08 Views5339
    Read More
  9. Spring Security Basic 인증 비활성화 설정

    Date2016.08.18 Views5556
    Read More
  10. Spring 개발을 위한 Eclipse 개발 환경 구성하기

    Date2016.12.06 Views5432
    Read More
  11. [패스트캣]1. 시작하기 - 설치

    Date2016.12.08 Views5982
    Read More
  12. [펌]자바 Crawling(크로울링) Ajax로 요청하는 것 긁어오기

    Date2018.07.04 Views2898
    Read More
  13. [펌]자바 Crawling(크로울링) 로그인 해 긁어오기

    Date2018.07.04 Views6018
    Read More
  14. 검색엔진 (1) - 검색엔진의 이해

    Date2016.12.08 Views6033
    Read More
  15. 검색엔진 (2) - 라이브러리: Lucene, Solr, Elasticsearch

    Date2016.12.08 Views6228
    Read More
  16. 루씬 기본적인 검색 엔진 - 샘플 예제

    Date2016.12.08 Views7771
    Read More
  17. 스프링 2.5.2 설치및 HelloWorld 출력하기

    Date2016.12.06 Views5836
    Read More
  18. 스프링 AOP의 주요 용어

    Date2016.08.18 Views5829
    Read More
  19. 스프링 Bean 객체의 초기화 및 소멸시 호출 메서드

    Date2016.08.18 Views4996
    Read More
  20. 스프링 XML 설정에서 자바 설정 Import하기

    Date2016.08.18 Views5071
    Read More
Board Pagination Prev 1 2 Next
/ 2

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved