Tuesday, 26 May 2020

Bring Data to HTML using XMLHttpRequest

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>

<script>
  function GetData() {
  var req = new XMLHttpRequest();
  req.open ('GET','https://jsonplaceholder.typicode.com/posts');
  req.onload = function () {
    var data = JSON.parse(req.responseText);
    RenderHtml (data);
}
  req.send();
}

function RenderHtml(data) {
console.log (data)
var dt = "";

for (i=0; i<data.length; i++) {
  dt += "<p>" + data[i].title + "</p>";
    }
  document.getElementById ('dataTable').innerHTML = dt;
}
</script>
</head>

<body>
<div id="dataTable">Nothing yet...</div>
<button onclick="GetData()">Get Data</button>
</body>
</html>

No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...