gpt4 book ai didi

javascript - 我的 api 请求没有返回任何内容,也没有错误消息

转载 作者:行者123 更新时间:2023-11-28 17:51:02 24 4
gpt4 key购买 nike

当我运行这个文件时,我什么也没得到。如果我改为运行 console.log(getInfo());最后我得到 Promise <pending> 。请帮忙。

function getInfo(){
var url = `https://api.nutritionix.com/v1_1/search/cheddar%20cheese?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat&appId=${apiId}&appKey=${apiKey}`;
return(
fetch(url)
.then(data=>{

return JSON.parse(data);
})
);
}

getInfo().then(result =>{
console.log(result);

最佳答案

这不是您使用 fetch API 的方式。像这样使用 response.json() (记录错误,因为我不知道 apiIdapiKey):

function getInfo(){
var apiId = 1;
var apiKey = 1;
var url = `https://api.nutritionix.com/v1_1/search/cheddar%20cheese?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat&appId=${apiId}&appKey=${apiKey}`;
return fetch(url).then(response => response.json());
}

getInfo().then(data => console.log(data));

关于javascript - 我的 api 请求没有返回任何内容,也没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45524448/

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