fetch API 소스 코드 저장

jaybon·2023년 6월 7일
0

자바스크립트

목록 보기
7/8

Get

일반

    fetch("/api/v1/admin/notice", {
        method: "GET"
    })
        .then(res => res.json())
        .then(result => {
            if (result.code === 0) {
                tuiGridNotice.resetData(result.data.noticeList);
            } else {
                alert(result.message);
            }
        })
        .catch(error => {
            console.log(error);
        });

blob

const downloadFile = (blob, name) => {
    const anchorElement = document.createElement("a");
    const localUrl = window.URL.createObjectURL(blob);
    anchorElement.href = localUrl;
    anchorElement.download = name;
    anchorElement.style.display = 'none';
    document.body.appendChild(anchorElement);
    anchorElement.click();
    document.body.removeChild(anchorElement);
    window.URL.revokeObjectURL(localUrl); // cleanup - 쓰임을 다한 url 객체 삭제
}


    fetch(url)
        .then(res => res.blob())
        .then(result => {
            downloadFile(result, name);
        })
        .catch(error => {
            alert("An error occurred during file download.");
        });

Post

    fetch("/api/v1/admin/notice", {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(reqAdminAddNoticeDTO)
    })
        .then(res => res.json())
        .then(result => {
            alert(result.message);

            if (result.code === 0) {
                closeModal();
                getNoticeListAndSetGridRows();
            }
        })
        .catch(error => {
            console.log(error);
        });

Put

    fetch("/api/v1/admin/notice", {
        method: "PUT",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(reqAdminEditNoticeDTO)
    })
        .then(res => res.json())
        .then(result => {
            alert(result.message);
            if (result.code === 0) {
                closeModal();
                getNoticeListAndSetGridRows();
            }
        })
        .catch(error => {
            console.log(error);
        });

Delete

    fetch("/api/v1/admin/notice/" + idx, {
        method: "DELETE"
    })
        .then(res => res.json())
        .then(result => {
            alert(result.message);
            if (result.code === 0) {
                getNoticeListAndSetGridRows();
            }
        })
        .catch(error => {
            console.log(error);
        });
profile
티스토리 블로그 https://ondolroom.tistory.com/

0개의 댓글