获取 json 到表(端点)

我是 javascript 的初学者,每天都在尝试学习,在这里我尝试从带有端点的 url 获取 json 数据,由于某种原因,数据没有进入“表”,我可以在表中看到它,但不能在表上看到它console.log。我的 json 数据如下所示:


{

  "id": "145127236",

  "mygoals": "success",

  "future": "high",

  "dole": {

    "Key": "fhd699f"

  }

}

我的代码是这样的:


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">


    <style>

    </style>

    

</head>

<body>


<div class="container">

    <table class="table table-stripped"> 

        <thead>

           <tr> 

               <th> emp id</th>

               <th> emp mygoals</th>

               <th> emp future</th>


           </tr> 

        </thead>

    

    

      <tbody id="data" >

 </tbody>

  </table

</div>



    <script>

  fetch("https://sp*****.com/get/henkilot/98765432/sijoitus",

              {

              method: "GET",

              headers: {

                 "x-api-key": "p*****w"

              }

            }

          ).then(res =>{ 


        res.json().then(

        data=> {

        console.log(data);

        if(data.length > 0){

        var temp ="";

        

        data.forEach((u) =>{

              temp +="<tr>";

              temp += "<td>"+u.id+"</td>";

              temp += "<td>"+u.mygoals+"</td>";

              temp += "<td>"+u.future+"</td></tr>";


        })

               document.getElementById("data").innerHTML = temp

        }

        }

      )

    }

  )

 .catch(err => {

          console.log("ERROR: " + err);

        });

    </script>


</body>

</html> 


喵喔喔
浏览 87回答 2
2回答

Smart猫小萌

您在问题中编写的 JSON 数据似乎不是数组,因此不需要迭代数据。一种选择是删除forEach或修复端点响应(如果您有权访问它)。这是认为端点不响应数组的代码:<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width">&nbsp; &nbsp; <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">&nbsp; &nbsp; <style>&nbsp; &nbsp; </style>&nbsp; &nbsp;&nbsp;</head><body><div class="container">&nbsp; &nbsp; <table class="table table-stripped">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th> emp id</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th> emp mygoals</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th> emp future</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <tbody id="data" >&nbsp;</tbody>&nbsp; </table></div>&nbsp; &nbsp; <script>&nbsp; fetch("https://asdasd.free.beeceptor.com/a",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: "GET",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"x-api-key": "p*****w"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ).then(res =>{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; res.json().then(&nbsp; &nbsp; &nbsp; &nbsp; data=> {&nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; var temp ="";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp +="<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp += "<td>"+data.id+"</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp += "<td>"+data.mygoals+"</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp += "<td>"+data.future+"</td></tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById("data").innerHTML = temp&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }&nbsp; )&nbsp;.catch(err => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("ERROR: " + err);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script></body></html>&nbsp;

绝地无双

你的牙套太疯狂了,试试:<!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width">&nbsp; <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"></head><body>&nbsp; <div class="container">&nbsp; &nbsp; <table class="table table-stripped">&nbsp;&nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th> emp id </th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th> emp mygoals </th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th> emp future </th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp;&nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; <tbody id="data"></tbody>&nbsp; &nbsp; </table>&nbsp; </div><script>&nbsp; const tableData = document.getElementById("data")&nbsp; &nbsp; ;&nbsp; fetch("https://sp*****.com/get/henkilot/98765432/sijoitus",&nbsp; &nbsp; { method: "GET"&nbsp; &nbsp; , headers: { "x-api-key": "p*****w" }&nbsp; &nbsp; })&nbsp; .then(res=>res.json())&nbsp; .then(data=>&nbsp; &nbsp; {&nbsp; &nbsp; console.log(data)&nbsp; &nbsp; data.forEach(u=>&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; let newRow = tableData.insertRow()&nbsp; &nbsp; &nbsp; newRow.insertCell().textContent = u.id&nbsp; &nbsp; &nbsp; newRow.insertCell().textContent = u.mygoals&nbsp; &nbsp; &nbsp; newRow.insertCell().textContent = u.future&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })&nbsp; .catch(err => console.log("ERROR: " + err) );</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5