본문 바로가기

vanila Javascript

[프론트개발] Json 응답, 요청 처리


function getCampaignInfo() {
    const promotion_uuid = 'PMT-D6AFE6BA-7D04-5939-2D97-61843A9584E2';
    // 통신에 사용될 XMLHttpRequest 객체
    const httpRequest = new XMLHttpRequest();
    // Post 방식으로 요청
    httpRequest.open("POST", `${baseApiUrl}event/custom/get`, true);
    // Response Type을 Json으로 사전 정의
    httpRequest.responseType = 'json';
    //정의된 서버에 Json 형식의 요청 Data를 포함하여 요청을 전송
    httpRequest.send(JSON.stringify({'promotion_uuid' : promotion_uuid}));
    //readyState가 Done이고 응답 값이 200일 때,  response로 data 가져오기
    httpRequest.onload = function() {
        httpRequest.status === 200 ? buildInfo(httpRequest.response.data) : alert('목록을 불러오지 못했습니다.');
        console.log(httpRequest.response.data)
    };
}