inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

REST API

highlrang
1

 

 

 

 

$.ajax({
    type: "GET",
    url: `/api/board/${id}`,
    dataType: "json",
    success: function(result){
        // 반환된 객체 처리
        // 객체 상태에 따라 성공, 에러 분기 처리
    }
});
$.ajax({
    type: "POST",
    url: "/api/board",
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf8",
    dataType: "json",
    success: function(result){
        location.href=`/board/detail/${result.body}`;
        // result.body == entity의 id
        // 서버에서 return id하고 dataType text로 받으면 result == id
    },
    error: function(error){
        // http status를 200으로 보내지 않으면 해당 분기 타게 됨
        // 반환 객체는 responseText에 text로 담기기 때문에 JSON으로 변환 시켜줌

        let errRes = JSON.parse(error.responseText); 
        alert(errRes.body);
 
    }
});
$.ajax({
    type: "POST",
    url: "/api/board",
    data: new FormData($("form")[0]),
    enctype: "multipart/form-data",
    contentType: false,
    processData: false,
    dataType: "json",
    success: function(result){
    }

});

 

REST_API

답변 0