标签 javascript 下的文章

async function http(obj){
    let {method,url,params,body,data} = obj;
    if(params){
        url += '?' + new URLSearchParams(params).toString();
    }
    if(body){
        url += '/'+body;
    }
    if(data){
        const res = await fetch(url,{
        method: method,
        headers:{
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data)
        })
        return await res.json();
    }
    else{
        const res = await fetch(url,{
        method: method,
        headers:{
            'Content-Type': 'application/json',
        },
        })
        return await res.json();
    }
    }