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=>{
//相关逻辑
})

猜你喜欢

转载自blog.csdn.net/weixin_44758548/article/details/105004502