art*_*boy 2 javascript fetch-api
我想了解两种get方法之间的区别,一种工作,另一种不是,但是我不明白为什么。
这不起作用:
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks', {
method: 'GET',
}).then(res => res.json)
.catch(error => {
console.error('Error:', error);
})
.then(response => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
然后返回:
ƒjson(){[本地代码]}
这很好用:
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks').then(function(response){
response.json().then(function(data) {
console.log(data);
});
}).catch(function(error) {
console.log('Fetch Error:', error);
});
Run Code Online (Sandbox Code Playgroud)
然后返回:
{tasks:Array(4)}任务:(4)[{…},{…},{…},{…}] 原型:对象
如果您想尝试一下:
fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks', {
method: 'GET',
}).then(res => res.json)
.catch(error => {
console.error('Error:', error);
})
.then(response => {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为您正在返回该res.json函数。您必须调用它并返回一个Promise:
.then(res => res.json())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3641 次 |
| 最近记录: |