[removed] Fetch DELETE and PUT requests

前端 未结 7 2066
一个人的身影
一个人的身影 2020-12-01 04:26

I have gotten outside of GET and POST methods with Fetch. But I couldn\'t find any good DELETE and PUT example.

So, I ask you for it. Could you give a good example

7条回答
  •  情歌与酒
    2020-12-01 04:47

    Some examples:

    async function loadItems() { try { let response = await fetch(https://url/${AppID}); let result = await response.json(); return result; } catch (err) { } }

    async function addItem(item) {
        try {
            let response = await fetch("https://url", {
                method: "POST",
                body: JSON.stringify({
                    AppId: appId,
                    Key: item,
                    Value: item,
                    someBoolean: false,
                }),
                headers: {
                    "Content-Type": "application/json",
                },
            });
            let result = await response.json();
            return result;
        } catch (err) {
        }
    }
    
    async function removeItem(id) {
        try {
            let response = await fetch(`https://url/${id}`, {
                method: "DELETE",
            });
        } catch (err) {
        }
    }
    
    async function updateItem(item) {
        try {
            let response = await fetch(`https://url/${item.id}`, {
                method: "PUT",
                body: JSON.stringify(todo),
                headers: {
                    "Content-Type": "application/json",
                },
            });
        } catch (err) {
        }
    }
    

提交回复
热议问题