import {Message} from 'element-ui';
interface Result {
  msg?:string,
  [propName:string]:any,
}
const handleState:(data:Response) => Promise<any> = async (data:Response):Promise<any> => {
  const {status} = data;
  const result:Result = await data.json();
  if(status === 200) {
    return Promise.resolve(result);
  }
  const {msg} = result;
  if(msg) {
    Message.error(msg);
    return Promise.reject(result);
  }
  Message.error('服务器错误');
  return Promise.reject(result);
}

export const postRequest: (url:string, data: any) => Promise<any> = async (url:string, data:any = {}) :Promise<any> => {
  try {
    const result: Response = await fetch(url, {
      body: JSON.stringify(data),
      method: 'POST',
      headers: new Headers({
        'Content-Type': 'application/json',
        'Authorization': Cookies.get('token') || '',
      })
    })
    return handleState(result);
  } catch (error) {
    return Promise.reject(error);
  }
};
export const getRequest = () => {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 浏览器兼容性

兼容性感人!!!

Last Updated: 4/27/2020, 10:56:04 PM