获取api和json

问题描述 投票:-1回答:1

由于某种原因,我的js文件行12中的第二个命令似乎是非导数的。这是为什么 ?谢谢

编辑:问题应该是:由于某种原因,第二个console.log没有提供输出。

评论员已成功回答了这个问题。谢谢

codepen

document.getElementById('btn').addEventListener('click', btnPressed);

function btnPressed() {

  fetch("https://randomuser.me/api")
    .then(function(res) {
      console.log(res);
      return res.json();

    })
    .then(function(data) {
      console.log(data);
    })
    .catch(err => console.log('Error,with     message:', err.statusText))
}
<button id=btn>Button</button>
json fetch-api console.log
1个回答
0
投票
Both console.log functions are working... Please run the below code.



    document.getElementById('btn').addEventListener('click', btnPressed);

    function btnPressed() {

      fetch("https://randomuser.me/api")
        .then(function(res) {
console.log("...................1st console Start");
          console.log(res);
console.log("...................1st console End");
          return res.json();

        })
        .then(function(data) {
console.log("...................2nd console Start");
          console.log(data);
console.log("...................2nd console End");
        })
        .catch(err => console.log('Error,with     message:', err.statusText))
    }
    <button id=btn>Button</button>
© www.soinside.com 2019 - 2024. All rights reserved.