Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Parse JSON objects</title>
  7. </head>
  8.  
  9. <body>
  10. <button onclick="postFunction()">Posts</button>
  11. <ul id="post"></ul>
  12.  
  13. <button onclick="commentFunction()">Comments</button>
  14. <ul id="comment"></ul>
  15.  
  16. <button onclick="profileFunction()">Profiles</button>
  17. <ul id="profile"></ul>
  18.  
  19. <script>
  20. function postFunction() {
  21. var xmlhttp = new XMLHttpRequest();
  22. xmlhttp.onreadystatechange = function () {
  23. if (this.readyState == 4 && this.status == 200) {
  24. let myArr = JSON.parse(this.responseText);
  25. let output = '';
  26. for (var i = 0; i < myArr.length; i++) {
  27. output += '<li>' + 'postId: ' + myArr[i].id + '<br>' + ' title: ' + myArr[i].title + '<br>' + ' author: ' + myArr[i].author; '</li>'
  28. }
  29. document.getElementById('post').innerHTML = output;
  30. }
  31. };
  32. xmlhttp.open("GET", "http://35.246.169.20:3000/posts", true);
  33. xmlhttp.send();
  34. }
  35.  
  36. function commentFunction() {
  37. var xmlhttp = new XMLHttpRequest();
  38. xmlhttp.onreadystatechange = function () {
  39. if (this.readyState == 4 && this.status == 200) {
  40. let myArr = JSON.parse(this.responseText);
  41. let output = '';
  42. for (var i = 0; i < myArr.length; i++) {
  43. output += '<li>' + 'commentId: ' + myArr[i].id + '<br>' + ' body: ' + myArr[i].body + '<br>' + ' postId: ' + myArr[i].postId; '</li>'
  44. }
  45. document.getElementById('comment').innerHTML = output;
  46. }
  47. };
  48. xmlhttp.open("GET", "http://35.246.169.20:3000/comments", true);
  49. xmlhttp.send();
  50. }
  51.  
  52. function profileFunction() {
  53. var xmlhttp = new XMLHttpRequest();
  54. xmlhttp.onreadystatechange = function () {
  55. if (this.readyState == 4 && this.status == 200) {
  56. var myObj = JSON.parse(this.responseText);
  57. document.getElementById("profile").innerHTML = '<li>' + 'name: ' + myObj.name; '</li>'
  58.  
  59. }
  60. };
  61. xmlhttp.open("GET", "http://35.246.169.20:3000/profile", true);
  62. xmlhttp.send();
  63. }
  64. </script>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement