fecth

https://blog.csdn.net/qq_24713843/article/details/78866713

这篇文章很不错!

export function post(url,paramsObjs) {
  var result = fetch(url, {
    method: 'POST',
    mode : 'no-cors',
    // credentials: 'include',
    headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/x-www-form-urlencoded'
    }, // 注意 post 时候参数的形式 body: "a=100&b=200" });
    body: obj2params(paramsObjs)
  })
  return result;
export function get(url) {
  var result = fetch(url,{
    credentials:'include',
    headers:{
      "Accept":"application/json,text/plain.*/*"
    }
  });
  return result;
}

fecth返回的数据可以用.then .catch 来处理

resultHandle(result){
    result.then(res=>{
      if(res.ok){
        return res.json()
      }else{
        console.log("当前城市:"+this.props.cityName);
        console.log("当前页码:"+this.state.page);
        return ListData;
      }
    }).then(json=>{
      const data = json.data;
      const hasMore = json.hasMore;
      this.setState({
        hasMore:hasMore,
        data:this.state.data.concat(data),
        isLoadingMore:false
      })
    }).catch(err=>{
      console.log(err.message);
    })
  }

你可能感兴趣的:(fecth)