-
AJAX parameter 타입 별 처리 방법JS 2023. 2. 13. 16:09
POST
1. String, int, boolean 등 단일 변수를 전송할 때
$.ajax({ type: 'POST', async: true, url: "url주소", data: {test: "test"}, success: function(response) { alert(response.data); }, error: function(error) { console.log(error); } })
@ResponseBody @RequestMapping(value = "/{workspace}/system/control/testMethod", method = RequestMethod.POST, produces = "application/json;charset=utf8") public String testMethod(String test) { try { return test; } catch (Exception e) { e.printStackTrace(); return "false"; } }
간단하게 파라미터로 변수를 받아주면 끝
2. 여러 변수들을 받을 때
2-1) 따로 따로 받기
$.ajax({ type: 'POST', url: "url 주소", data: {fruit: "grape", box: 2, order: true}, datatype: "text", success: function(response) { alert(response) }, error: function(error) { console.log(error); } })
@ResponseBody @RequestMapping(value = "url 주소", method = RequestMethod.POST) public String testMethod(@RequestParam String fruit, @RequestParam int box, @RequestParam boolean order){ try { String result = fruit + box; return result; } catch (Exception e) { e.printStackTrace(); return "false"; } }
보낼 때 datatype를 "text"로 설정했기 때문에
java 메소드에서 produces = "application/json;charset=utf8" 도 삭제해줘야 에러없이 동작한다.
2-2) Map으로 받기
$.ajax({ type: 'POST', url: "url 주소", data: {fruit: "grape", box: 2, order: true}, success: function(response) { alert(response) }, error: function(error) { console.log(error); } })
@ResponseBody @RequestMapping(value = "url 주소", method = RequestMethod.POST) // , produces = "application/json;charset=utf8" public String testMethod(@RequestParam Map<String, Object> map) { try { String result = (String)map.get("fruit") + Integer.parseInt(map.get("box").toString()) + " : " + Boolean.parseBoolean(map.get("order").toString()); return result; } catch (Exception e) { e.printStackTrace(); return "false"; } }
Map<String, Object> 형태로 가지고 오면 Object형을 형변환 해주는게 좀 일이다
2-3) VO로 받기
public class parameterVO { String fruit; int box; boolean order; public String getFruit() { return fruit; } public void setFruit(String fruit) { this.fruit = fruit; } public int getBox() { return box; } public void setBox(int box) { this.box = box; } public boolean isOrder() { return order; } public void setOrder(boolean order) { this.order = order; } }
$.ajax({ type: 'POST', url: 'url 주소', data: {fruit: "grape", box: 2, order: true}, success: function(response) { alert(response) }, error: function(error) { console.log(error); } })
@ResponseBody @RequestMapping(value = "url 주소", method = RequestMethod.POST) // , produces = "application/json;charset=utf8" public String testMethod(ParameterVO pvo) { try { String result = "ParameterVO ! " + pvo.getFruit() + pvo.getBox() + " : " + pvo.isOrder(); return result; //return fruit + box + " : " + order; } catch (Exception e) { e.printStackTrace(); return "false"; } }
@RequestBody : http request body의 json value를 자바 객체와 바인딩 해준다
(HttpMessageConverter : 자바 객체와 관련된 http 리퀘스트 메시지를 변환)
-> @RequestBody 어노테이션을 쓸 때는 JSON, 쓰지 않을 땐 x-www-form-urlencoded 방식으로 파라미터를 리퀘스트할 것!
@RequestParam : 리퀘스트 파라미터와 메소드 파라미터를 바인딩 해준다@RequestBody를 같은 메소드에서 2번 사용할 수 없다
public String testMethod(@RequestBody ParameterVO pvo, @RequestBody parameterVO2 pvo2)
처럼 사용할수 X
'JS' 카테고리의 다른 글
JQuery JS keyup 합산 이벤트 (0) 2022.10.13 Video.js / videojs-yotube : video js로 유튜브 영상 재생하기 (0) 2022.09.11 c3.js 차트 라이브러리 (0) 2022.06.06 NodeJS : nodemailer을 이용한 이메일 전송 (0) 2022.05.01 21-12-09 제이쿼리 이벤트 (0) 2021.12.09