gpt4 book ai didi

javascript - JS - 获取 API,GET 方法返回 "ƒ json() { [native code] }"

转载 作者:行者123 更新时间:2023-12-01 11:23:55 40 4
gpt4 key购买 nike

我想了解两种 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);
});

并返回:

ƒ json() { [native code] }



这很好用:
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);
});

并返回:

{tasks: Array(4)} tasks : (4) [{…}, {…}, {…}, {…}] proto : Object



如果你想试试:

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("first way");
console.log(response);
});

fetch('https://glo3102lab4.herokuapp.com/fee958c0-c320-40d0-a750-218f2d7c1303/tasks').then(function(response){
response.json().then(function(data) {
console.log("second way");
console.log(data);
});
}).catch(function(error) {
console.log('Fetch Error:', error);
});

最佳答案

它不起作用,因为您要返回 res.json功能。你必须调用它并返回一个 Promise:

.then(res => res.json())

关于javascript - JS - 获取 API,GET 方法返回 "ƒ json() { [native code] }",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48774535/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com