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

Posted

tags:

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

table {
  margin-top: 20px;
}
table, tr, td {
  border: 1px solid;
} 
  function search() {  
    
    // Relevant documentation:
    // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
    
    
    var queryURL = "https://jsonplaceholder.typicode.com/users";
    
    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);
      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.querySelector("#users");
    usersDiv.innerHTML = "";
  
    // creates and populate the table with users
    var table = document.createElement("table");
          
    // iterate on the array of users
    users.forEach(function(currentUser) {
        // creates a row
        var row = table.insertRow();
        // insert cells in the row
        var nameCell = row.insertCell();
        nameCell.innerHTML = currentUser.name;
        var cityCell = row.insertCell();
        cityCell.innerHTML = currentUser.address.city;
     });
  
     // adds the table to the div
     usersDiv.appendChild(table);
}

JS.JSON.RetrievingData.XHR2.API.v1
----------------------------------


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

[License](https://codepen.io/Onlyforbopi/pen/yRLvEr/license).
<!DOCTYPE html>
<html lang="en">
<head>
<title>Working with remote data suing XhR2</title>
  <meta charset="utf-8"/>
</head>
<body>
  <p>Working with remote data suing XhR2</p>
  <button onclick="search();">Get a remote list of users' names and emails</button>
  <div id="users"></div>
</body>
</html>

以上是关于html JS.JSON.RetrievingData.XHR2.API.v1的主要内容,如果未能解决你的问题,请参考以下文章

html Html模板/ Html Boilerplate |标签HTML

html里怎么引用一个html的头部

html5与传统html区别

html4和html5的区别

HTML元素

head标签怎么给多个html引用