fetch
fetch() 必须接受一个参数——资源的路径。无论请求成功与否,它都返回一个 Promise 对象,resolve 对应请求的 Response
一旦 Response 被返回,就可以使用一些方法来定义内容的形式,以及应当如何处理内容
fetch('/asimov/subscriptions/recommended_collections').then(response => {
return response.json();
}).then(value => {
console.log(value);
});
fetch成功出发then,它里面有个response,response拿到的是一个对象,这个对象里面它的body获得的是响应主体信息,但body是一个可读流(ReadableStream),把拿到的信息变成json格式的对象,里面有一个方法叫json()可以变成json格式对象返回结果是promise实例(因为返回结果可能是成功或者失败)
console.log(fetch('./1.json'));
fetch('./1.json').then(response=>{
console.log(response);
});
fetch('./1.json').then(response=>{
console.log(response.json());
});
fetch('./1.json').then(response=>{
returnresponse.json();
}).then(result=>{
console.log(result);
});