const putMethod = {
method: 'PUT', // Method itself
headers: {
'Content-type': 'application/json; charset=UTF-8' // Indicates the content
},
body: JSON.stringify(someData) // We send data in JSON format
}
// make the HTTP put request using fetch api
fetch(url, putMethod)
.then(response => response.json())
.then(data => console.log(data)) // Manipulate the data retrieved back, if we want to do something with it
.catch(err => console.log(err)) // Do something with the error
{
"posts": [
"id": 1,
"title": "Some Title", // what we typed in the title input field
"body": "Some Body", // what we typed in the body input field
]
}
对于删除方法,我们有:
const deleteMethod = {
method: 'DELETE', // Method itself
headers: {
'Content-type': 'application/json; charset=UTF-8' // Indicates the content
},
// No need to have body, because we don't send nothing to the server.
}
// Make the HTTP Delete call using fetch api
fetch(url, deleteMethod)
.then(response => response.json())
.then(data => console.log(data)) // Manipulate the data retrieved back, if we want to do something with it
.catch(err => console.log(err)) // Do something with the error
5条答案
按热度按时间u91tlkcl1#
下面是一个FETCH
POST
示例。您可以对DELETE
执行相同的操作。jvidinwx2#
好的,下面也是一个FETCH
DELETE
示例:brccelvz3#
对于PUT方法,我们有:
例如,对于某些数据,我们可以有一些输入字段或您需要的任何内容:
在我们的
data base
中,它将采用json
格式:对于删除方法,我们有:
在URL中,我们需要键入删除的ID:
https://www.someapi/id
cfh9epnr4#
只是简单的回答。获取删除
uqdfh47h5#
下面是使用FETCH API的CRUD操作的很好的示例:
《关于如何使用Fetch API执行HTTP请求的ES6实用指南》,作者:Dler Ari https://link.medium.com/4ZvwCordCW
以下是我尝试修补或放置的示例代码
对于删除:
有关更多信息,请访问Using Fetch-Web API|MDN https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch>Fetch_API。