fetch基础学习

75 阅读1分钟

第三方文档

fetch==>链接

基本封装

  //fetch基础学习
  async function fetchHttp(options){
    console.log(options)
    let {url,method,params,data}=options
    if(params){
      const str = new URLSearchParams(params).toString()
      url+=`?${str}`
    }
    let res;
    if(data){
      res=await fetch(url,{
        method,
        headers:{
         'Content-Type': 'application/json'
        },
        body:JSON.stringify(data)
      })
    }else{
       res=await fetch(url)
    }
    return res.json()
  }