【问题标题】:the fetch response respond with empty object Promise获取响应以空对象 Promise 响应
【发布时间】:2018-03-23 15:29:48
【问题描述】:

我正在使用来自 GitHub 的 API 来获取组织的存储库,但是当我使用 fetch 时 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" }

【问题讨论】:

    标签: javascript fetch


    【解决方案1】:

    您需要使用两个.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);
    });
    

    【讨论】:

    • 谢谢,你救了我的命 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多