Fetch 获取数据

Fetch 获取数据时返回promise, promise 的值是response

response 继承body, body 是mixin, 类似于Java 的抽象类

body 有json() 和text() 方法可以把body 中的ReadableStream 数据转化为json 或文本

fetch('<https://api.github.com/users/bifan/repos>')
  .then(response => response.json())
  .then(data => {
    // Here's a list of repos!
    console.log(data)
  });

Fetch 发送数据

Fetch 发送数据需要设置三个选项

let content = {some: 'content'};

// The actual fetch request
fetch('some-url', {
  method: 'post',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(content)
})
// .then()...

处理异常情况