참고 – 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; } }
- SpringBoot Port 변경 2022년 12월 27일
- jQuery checkbox 전체 선택/해제 예제 2022년 3월 15일
- Highcharts error #17 2019년 7월 10일
- Highchart TypeError: c.init is not a function 2019년 7월 10일
- Highchart 크기 조정 2019년 7월 8일
- Highcharts error #13 2019년 7월 8일
- Spring 트랜잭션 관리 2019년 6월 20일
- Spring RESTful 웹 서비스 사용하기 2019년 6월 19일
- Spring RESTful 웹 서비스 2019년 6월 19일
- Spring Scheduling Task 예제 2019년 6월 18일
- Grid Stack Two grids demo 예제 2019년 6월 12일
- 동적으로 추가/생성된 태그에 이벤트 추가 방법 2019년 6월 10일
- Highcharts update event 2019년 3월 26일
- spring mybatis mysql 연결 예제 2018년 5월 4일
- Spring Path Parameter 예제 2018년 2월 13일
- AngularJS $http, spring TEXT response 예제 2018년 1월 17일
- jdom2를 이용한 spring xml response(응답) 2017년 12월 5일
- jquery xml 요청, spring xml 리턴 2017년 12월 5일
- spring controller에서 get, post 동시 허용 방법 2017년 12월 5일
- mybatis, oracle procedure(오라클 커서)를 통한 데이터 조회 2017년 11월 6일
- HTTP 406 Not Acceptable in Spring, Jackson, Json 2017년 11월 6일
- jquery div show hide 예제 2016년 5월 25일
- jquery css 예제 2016년 5월 25일
- jquery selectbox 예제 2016년 5월 25일
- jquery json 예제 2016년 5월 25일
- jquery template 예제 1 2016년 5월 25일
- jquery datepicker 2016년 5월 9일
- ServletContextLister를 구현한 웹 애플리케이션 시작 시 작업 2016년 5월 2일
- jquery radiobox 2016년 4월 29일
- Web Application MessageResource 2016년 3월 8일
- org.mariadb.jdbc.internal.common.QueryException : No database selected 2016년 2월 19일
- jquery bgiframe 정리 2016년 2월 1일
- jquery each( $.each ) 2016년 1월 25일
- jquery spring – checkbox로 선택된 배열 주고 받기 2015년 8월 15일
- jquery spring ajax call 2015년 8월 15일
- spring json return 2015년 8월 15일
- spring exception – SimpleMappingExceptionResolver 2015년 8월 8일
- jQuery 동적 체크박스 제어 2015년 1월 27일
- 간단한 jquery chained selectbox 예제 2014년 12월 4일
- jquery ui tip 2014년 9월 2일