ES6第七天学习fetch增删改查

  • fetch封装:easyhttp.js
/*
 
 * 分析:封装fetch:更快,更简单的请求数据
 * @version 1.0.0
 * @author yanmeiliang1
 * @liense 
 * */
class EasyHttp{
    //get请求方式
    get(url){        
    return new Promise((resolve,reject)=>{
        fetch(url)
            .then(res=>res.json())
            .then(data=>resolve(data))
            .catch(err=>reject(err))
        })
    }    
    //post请求
    post(url,data){        
    return new Promise((resolve,reject)=>{
        fetch(url,{
            method:"POST",
            headers:{'Content-Type':'application/json'},
            body:JSON.stringify(data)
        })
            .then(res=>res.json())
            .then(data=>resolve(data))
            .catch(err=>reject(err))
        })
    }
    //put更新
    put(url,data){        
    return new Promise((resolve,reject)=>{
        fetch(url,{
            method:"PUT",
            headers:{'Content-Type':'application/json'},
            body:JSON.stringify(data)
        })
            .then(res=>res.json())
            .then(data=>resolve(data))
            .catch(err=>reject(err))
        })
    }
    //delet
   delete(url){        
    return new Promise((resolve,reject)=>{
        fetch(url,{
            method:"DELETE",
            headers:{
                'Content-type':'application/json'
            }
            
        })
            .then(res=>res.json())
            .then(data=>resolve('数据删除成功'))
            .catch(err=>reject(err))
        })
    }
}


  • 调用接口:app1.js
const http=new EasyHttp;
//get请求数据
//const users=http.get("http://jsonplaceholder.typicode.com/users")
//.then((data)=>{
//    console.log(data);
//})
//.catch(err=>console.log(err))
//;
//console.log(users.then(data=>console.log(data)));
//http.get("http://jsonplaceholder.typicode.com/users");

//传输数据put user
const data={
    name:"Henry",
   username:"米斯特五",
   email:"1234@qq.com"
    
};

//http.post("http://jsonplaceholder.typicode.com/users",data)
//.then(data=>console.log(data))
//.catch(err=>console.log(err));

//结果:{name: "Henry", username: "米斯特五", email: "1234@qq.com", id: 11}


//update user
//http.put("http://jsonplaceholder.typicode.com/users/2",data)
//.then(data=>console.log(data));
//.catch(err=>console.log(err));
//结果:{name: "Henry", username: "米斯特五", email: "1234@qq.com", id: 2}


//delet user
http.delete("http://jsonplaceholder.typicode.com/users/2")
.then(data=>console.log(data))
.catch(err=>console.log(err));
//结果:数据删除成功
  • html:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
         <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <meta  http-equiv="X-UA-Compatible" content="ie=edge"/>
    </head>
    <body>
        <script type="text/javascript" src="easyhttp.js"></script>
        <script type="text/javascript" src="app1.js"></script>
    </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值