html JS.JSON。使用XhR2 API获取远程JSON数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html JS.JSON。使用XhR2 API获取远程JSON数据相关的知识,希望对你有一定的参考价值。

table {
  margin-top: 20px;
}
table, tr, td {
  border: 1px solid;
  border-collapse: collapse
} 
  function search() {    
    var queryURL = "https://gist.githubusercontent.com/heiswayi/7fde241975ed8a80535a/raw/ff1caaeaf62bd6740ab7cafcd61f1215de173379/datatables-data.json";
    
    var xhr = new XMLHttpRequest();
    xhr.open('GET', queryURL, true);

    // called when the response is arrived
    xhr.onload = function(e) {
      var jsonResponse = xhr.response;
 
      // turn the response into a JavaScript object
      var users = JSON.parse(jsonResponse);
      console.log(users)
      displayUsersAsATable(users);
    }
    
    // in case of error
    xhr.onerror = function(err) {
      console.log("Error: " + err);
    }
    
    // sends the request
    xhr.send();
} 
  
function displayUsersAsATable(users) {
    // users is a JavaScript object
  
    // empty the div that contains the results
    var usersDiv = document.getElementById("users");
    usersDiv.innerHTML = "";
  
    // creates and populate the table with users
    var table = document.createElement("table");
  
  //Creating a Header//
  var header = table.insertRow();  
  var nameH = header.insertCell();
  nameH.innerHTML = "Name";
  var Position = header.insertCell();
  Position.innerHTML = "Position";
  var city = header.insertCell();
  city.innerHTML = "City";
  var aage = header.insertCell();
  aage.innerHTML = "Age"
  var Datein = header.insertCell();
  Datein.innerHTML = "Date in";
  var MSalary = header.insertCell();
  MSalary.innerHTML = "Year Salary";
    
    // iterate on the array of users
    users.data.forEach(function(currentUser) {
        // creates a row
        var row = table.insertRow();
        // insert cells in the row
        var nameCell = row.insertCell();
        nameCell.innerHTML = currentUser[0];
        var Jobcell = row.insertCell();
        Jobcell.innerHTML = currentUser[1]
        var cityCell = row.insertCell();
        cityCell.innerHTML = currentUser[2];
        var Age = row.insertCell();
        Age.innerHTML = currentUser[3];
        var In = row.insertCell();
        In.innerHTML = currentUser[4];
        var Salary = row.insertCell();
        Salary.innerHTML = currentUser[5];
     });
  
     // adds the table to the div
     usersDiv.appendChild(table);
}

JS.JSON.Getting remote JSON data with the XhR2 API
--------------------------------------------------


A [Pen](https://codepen.io/Onlyforbopi/pen/bmbWEK) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).

[License](https://codepen.io/Onlyforbopi/pen/bmbWEK/license).
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doing the assignment</title>
  <meta charset="utf-8"/>
</head>
<body>
  <p>Optional Project</p>
  <button onclick="search();">Create a table from remote data</button>
  <div id="users"></div>
</body>
</html>


以上是关于html JS.JSON。使用XhR2 API获取远程JSON数据的主要内容,如果未能解决你的问题,请参考以下文章

html JS.JSON。使用XhR2 API获取远程JSON数据

html 使用Xhr2 API JS.JSON.Downloading项目JSON数据

html JS.JSON.RetrievingData.XHR2.API.v1

html JS.JSON.AJAX.Request.Live编码视频,JSON与XhR2 API

html JS.JSON。使用fetch API获取远程JSON数据

php 下 html5 XHR2 + FormData + File API 上传文件