【发布时间】:2019-07-14 01:00:46
【问题描述】:
我正在学习 fetch api,对这段代码有点困惑:
fetch('/article/promise-chaining/user.json')
// .then below runs when the remote server responds
.then(function(response) {
// response.text() returns a new promise that resolves with the full response text
// when we finish downloading it
return response.text();
})
.then(function(text) {
// ...and here's the content of the remote file
alert(text); // {"name": "iliakan", isAdmin: true}
});
所以,从上面的代码可以看出,我们向某个位置发送 Get 请求。然后 fetch 返回带有响应对象的承诺,响应对象仅包含标题作为响应没有文件本身。那是对的吗?然后为了将文件本身作为文本获取,我们向服务器发出附加请求。那是对的吗?或者,如果我们想使用 response.json(),那么服务器会将解析后的 JSON 发送给我们
【问题讨论】:
标签: javascript