我已经通过 Fetch 摆脱了 GET 和 POST 方法。但我找不到任何好的 DELETE 和 PUT 示例。
所以,我向你求助。您能否给出一个带有 fetch 的 DELETE 和 PUT 方法的好例子。并解释一下。
最佳答案
这是一个获取POST
示例。您可以对DELETE
执行相同的操作。
function createNewProfile(profile) {
const formData = new FormData();
formData.append('first_name', profile.firstName);
formData.append('last_name', profile.lastName);
formData.append('email', profile.email);
return fetch('http://example.com/api/v1/registration', {
method: 'POST',
body: formData
}).then(response => response.json())
}
createNewProfile(profile)
.then((json) => {
// handle success
})
.catch(error => error);
关于Javascript:获取 DELETE 和 PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40284338/