基于 window下的 ajax ,fetch函数的封装 ES

shuke 2022-12-29 194

/*
* 基于 window下的 ajax ,fetch函数的封装
* auther:shuke
* date:20221229
* version:1.0
* */
const BASEURL="http://localhost:6600/api"
export default {
    getData(url,params={}){
        url=BASEURL+url
        const str=(Object.keys(params)).map((item)=> item+"="+params[item]).join("&")
        return fetch(url+"?"+str).then(res=>res.json())
    },
    postData(url,data={}){
        url=BASEURL+url
        console.log(url, data);
        return fetch(url,{
            method:"POST",
            body:JSON.stringify(data),
            headers:{
                "Content-type":"application/json"
            }
        }).then(res=>res.json())
    }
}


最新回复 (0)
全部楼主
返回