zoukankan      html  css  js  c++  java
  • fetch 函数分装

    1.fetch

    /**
     * 封装 fetch
     */
    import { hashHistory } from 'react-router';
     
    export default function request (method, url, body) {
      method = method.toUpperCase();
      if (method === 'GET') {
        // fetch的GET不允许有body,参数只能放在url中
        body = undefined;
      } else {
        body = body && JSON.stringify(body);
      }
     
      return fetch(url, {
        method,
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Access-Token': sessionStorage.getItem('access_token') || '' // 从sessionStorage中获取access token
        },
        body
      })
      .then((res) => {
        if (res.status === 401) {
          hashHistory.push('/login');
          return Promise.reject('Unauthorized.');
        } else {
          const token = res.headers.get('access-token');
          if (token) {
            sessionStorage.setItem('access_token', token);
          }
          return res.json();
        }
      });
    }
    
    // GET 请求
    export const get = url => request('GET', url);
    // POST 请求
    export const post = (url, body) => request('POST', url, body);
    // PUT 上传
    export const put = (url, body) => request('PUT', url, body);
    // DELETE 删除
    export const del = (url, body) => request('DELETE', url, body);

    .

  • 相关阅读:
    app 后端技术
    别为大公司拼命
    ifconfig 工具
    route工具
    ping 和 traceroute 命令
    IP路由选择
    TCP的那些事儿(下)
    TCP的那些事儿(上)
    Openresty 与 Tengine
    Excel.Application SaveAs 把excel转换为html
  • 原文地址:https://www.cnblogs.com/crazycode2/p/9068992.html
Copyright © 2011-2022 走看看