首页 > 其他 > 详细

fetch数据请求的封装

时间:2018-10-31 23:53:08      阅读:285      评论:0      收藏:0      [点我收藏+]
export default class HttpUtils {
    static get(url){
        return new Promise((resolve,reject)=>{
            fetch(url)
                .then(response=>response.json())
                .then(result=>{
                    resolve(result)
                })
                .catch(error=>{
                    reject(error)
                })
        })
    }
    static post(url,data){
        return new Promise((resolve,reject)=>{
            fetch(url,{
                method:‘post‘,
                header:{
                    ‘Accept‘:‘application/json‘,
                    ‘Content-Type‘:‘application/json‘
                },
                body:JSON.stringify(data)
            })
                .then(response=>response.json())
                .then(result=>{
                    resolve(result)
                })
                .catch(error=>{
                    reject(error)
                })
        })
    }
}

  

fetch数据请求的封装

原文:https://www.cnblogs.com/nixu/p/9886593.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!