我正在使用来自GitHub的API来获取组织的存储库,但是当我使用访存时,response.json()使用空对象进行响应,这是我的代码:

url = "https://api.github.com/orgs/octokit/repos";
fetch(url,{method: 'GET'})
.then((response)=>{console.log(response.json());})
.catch(()=>{console.log('err');});


响应为Promise { <state>: "pending" }

最佳答案

您需要使用两个.then(),第一个用于返回的Promise,第二个用于表示Promise的响应:

url = "https://api.github.com/orgs/octokit/repos";
fetch(url,{method: 'GET'})
.then(res => res.json())
.then(data => {
  console.log(data);
})
.catch(err => {
  console.log(err);
});

关于javascript - 获取响应以空对象Promise响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49453241/

10-12 01:15
查看更多