Fetch的四種請求

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=>{
//相關邏輯
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章