jquery spring ajax call

참고 – http://unoday.tistory.com/34

 

■ jquery ajax 호출
▷ post요청으로 data를 전송하고 리턴 타입은 text로 설정. 요청 url은 spring controller의 getText메소드를 호출.

$.ajax({
  url       : '/jqueryajax/getText' ,
  type	    : 'post'                ,
  data	    : 'name=aaa&age=28'	    ,
  dataType  : 'text'		    ,
  success   : function(data) {
    console.log('return string : ' + data);
  },
  error	: function() {
    console.log('error');
  }
});

▷ post요청으로 data를 전송하고 리턴 타입은 text로 설정. 요청 url은 spring controller의 getText메소드를 호출.
위의 요청과 다른점은 전송 data를 json 타입으로 하였음.

$.ajax({
  url       : '/jqueryajax/getText' ,
  type	    : 'post'                ,
  data	    : {'name':'bbb','age':29},
  dataType  : 'text'		    ,
  success   : function(data) {
    console.log('return string : ' + data);
  },
  error	: function() {
    console.log('error');
  }
});

▷ spring controller에서 ajax로 데이터를 리턴 받을 때 String type으로 받으면 한글이 깨져서 보인다. 이 때 spring controller에서 Map형식으로 리턴 받으면 한글이 깨지지 않는다. 이유는 모른다(ㅠㅠ).

$.ajax({
  // url이 '/jqueryajax/getText1' 이면 한글이 깨진다.
  url       : '/jqueryajax/getText2' ,
  type	    : 'post'                ,
  data	    : {'name':'한글','age':29},
  // 리턴 받는 dataType이 json이면 dataType을 json으로 변경
  dataType  : 'text'		    ,
  success   : function(data) {
    console.log('return string : ' + data);
    // 리턴 받는 dataType이 json이면 아래와 같이 data.name으로 변경
    console.log('return string : ' + data.name);
  },
  error	: function() {
    console.log('error');
  }
});

 

■ spring controller

@Controller("JqueryAjaxController.class")
@RequestMapping(value="/jqueryajax")
public class JqueryAjaxController {

  private static final Logger log = LoggerFactory.getLogger(JqueryAjaxController.class);

  @RequestMapping(value="/getText")
  public @ResponseBody String getText (HttpServletRequest request, SampleVO vo) throws Exception {

    log.debug(" getText name, age : {}, {}", vo.getName(), vo.getAge() );
    return "ajaxTextReturn name:"+vo.getName()+" age:" +vo.getAge();
  }

  // String 형식으로 리턴하고 클라이언트(jquery ajax)에서 text 형식으로 받으면 한글 데이터가 깨져 보이는 경우가 있다.
  @RequestMapping(value="/getText1")
  public @ResponseBody String getText1 (
      HttpServletRequest request, 
      HttpServletResponse response, 
      @RequestParam(value="name") String name,
      @RequestParam(value="age") int age ) throws Exception {
		
    log.debug( ">>> name="+name + "\tage="+age );
    return "name="+name + "\tage="+age;
		
  }
	
  // Map형식으로 리턴하고 JSON형식으로 클라이언트에서 받으면 데이터가 한글이더라도 깨지지 않는다.
  @RequestMapping(value="/getText2")
  public @ResponseBody Map<String,Object> getText2 (
      HttpServletRequest request, 
      HttpServletResponse response, 
      @RequestParam(value="name") String name,
      @RequestParam(value="age") int age ) throws Exception {
		
    log.debug( ">>> name="+name + "\tage="+age );
    Map<String,Object> map = new HashMap<String,Object>();
		
    map.put( "name", name);
    map.put( "age", age);
		
    return map;
  }
}

 

■ SampleVO value object

public class SampleVO {

  private String name;
  private int age;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다