美文网首页
fetch请求封装

fetch请求封装

作者: 甲乙丙丁0086 | 来源:发表于2019-01-02 17:19 被阅读0次

request 请求封装

const parseJSON = (response) => {
  const json = response.json();
  return json;
};

const checkStatus = (response) => {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  const error = new Error(response.statusText);
  error.response = response;
  throw error;
};

export default function request(url, options) {
  return fetch(url, options)
    .then(checkStatus)
    .then(parseJSON)
    .then(data => data)
    .catch(err => ({ err }));
}

相关文章

网友评论

      本文标题:fetch请求封装

      本文链接:https://www.haomeiwen.com/subject/upttrqtx.html