프로그래밍/- 남굼성의 Spring 교육
REST API 와 AJAX
즐겁게 하하하
2022. 3. 29. 16:18
728x90
객체를 JSON 문자열로 변경 > JSON.stringify()
JSON 문자열을 객체로 변환 > JSON.parse()
{ name: "jone" , age :30 } <=> '{ "name" : "John" , "age" : "30" }'
AJAX
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.1</version>
</dependency>
$.ajax({
type : "post",
url : "/ch4/send",
header : { "context-type" : "application/json" },
async : false,
dataType : "text",
data : JSON.stringify(person),
success : function( result) {
person2= JSON.parse(result);
alert(result);
data.html("person2 = " + person2 );
}, error : function() { alert("error") }
});
Controller에 @RequestBody 필수
동기 : 요청이 완료될때 까지 대기
비동기 : 요청후 다른작업 할수있음 ( 콜백함수 로 완료 알림 : success , error )
REST
프로토콜에 독립적 , HTTP 를 사용해서 구현
리소스 중심의 API 디자인 - HTTP 메서드로 수행할 작업을정의
리소스 : POST(쓰기) , GET(읽기) , PUT(수정) , DELETE(삭제)
읽기 | /comments | GET | 모든 댓글 보여줌 |
읽기 | /comments/{cno} | GET | 지정된 번호댓글 보여줌 |
쓰기 | /comments | POST | 새 댓글 저장 |
삭제 | /comments/{cno} | DELETE | 지정 댓글 삭제 |
수정 | /comments/{cno} | PUT | 수정댓글 저장 |
728x90