api.js 534 Bytes
Newer Older
牛文敏's avatar
init    
牛文敏 committed
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
function checkStatus(response) {
  return new Promise((resolve, reject) => {
    const json = response.json();

    // [200,299]
    if (response.ok) {
      return json.then(resolve, reject);
    }
    if(response.status !== 200){
      alert("接口请求失败!")
    }
    json.then(reject);
  });
}

export default function fetch(input, init) {
  if (!init) {
    init = {};
  }

  init.headers = {
    'Content-Type': 'application/json',
    ...init.headers
  };

  return window.fetch(`/${input}`, init).then(checkStatus);
}