Fetch的四種請求
記錄在寫前端頁面時所用fetch發送四種請求的寫法 注意.then({
})時response.json()與response.text()的區別: json( ):服務端返回json數據 text(
):服務端返回字符串格式
1.GET:
fetch('http://localhost:5555/dictItem/getAll')
.then(function(response){
return response.json();
})
.then(myJson=>{
//相關邏輯
})
2.POST:
fetch('http://localhost:5555/dictItem/addDict',{
method:'POST',
body:JSON.stringify(this.itemDialog),
headers:{
'content-Type':'application/json',
}
}).then(data=>{
return data.text();
}).then(ret=>{
//相關邏輯
})
3.PUT
fetch('http://localhost:5555/dictItem/editDict/',{
method:'put',
body:JSON.stringify(this.editForm),
headers: {
'Content-Type': 'application/json'
}
}).then(response=>{ return response.text()})
.then(ret=>{
//相關邏輯
})
4.DELETE
fetch('http://localhost:5555/dictItem/'+row.id,{
method:'delete'
}).then(res=>{return res.text()})
.then(ret=>{
//相關邏輯
})