Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. !DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  3. <head lang="en">
  4. <meta charset="UTF-8" />
  5. <title>Tasks</title>
  6. </head>
  7. <body>
  8.  
  9. <h2>Tasks</h2>
  10.  
  11. <!-- TODO: add the ability to list tasks -->
  12. <ul id="tasks">
  13. </ul>
  14.  
  15.  
  16. <form>
  17. <input type="text" name="name" id="name"/>
  18. <input type="button" onclick="addTask();" value="Add!"/>
  19. </form>
  20.  
  21. <!-- the javascript has been embedded to the same site -->
  22. <script th:inline="javascript">
  23. // The URL to the application server that holds the tasks.
  24. var url = /*[[@{/tasks}]]*/ null;
  25.  
  26. function loadTasks() {
  27. alert("hello world! it'd be nice if i would load the tasks from the server and show them on this page.");
  28.  
  29. const http = new XMLHttpRequest();
  30. http.open("GET", url, true);
  31. http.setRequestHeader("Content-type", "application/json");
  32.  
  33. http.onreadystatechange = function () {
  34. if (http.readyState === 4) {
  35. if (http.status === 200) {
  36. addTaskToList(JSON.parse(http.responseText));
  37. }
  38. }
  39. }
  40.  
  41. http.send(null);
  42. }
  43.  
  44. function addTask() {
  45. var name = document.querySelector("#name").value;
  46. if (!name) {
  47. return;
  48. }
  49.  
  50. console.log(name);
  51.  
  52. var http = new XMLHttpRequest();
  53. http.open("POST", url, true);
  54. http.setRequestHeader("Content-type", "application/json");
  55. var data = new Object();
  56. data.name = name;
  57.  
  58. http.onreadystatechange = function () {
  59. if (http.readyState === 4) {
  60. if (http.status === 200) {
  61. addTaskToList(JSON.parse(http.responseText));
  62. }
  63. }
  64. }
  65.  
  66. http.send(JSON.stringify(data));
  67. }
  68.  
  69.  
  70. function addTaskToList(task) {
  71. var liElement = document.createElement("li");
  72. liElement.appendChild(document.createTextNode(task.name));
  73. document.querySelector("#tasks").appendChild(liElement);
  74. }
  75.  
  76.  
  77. window.onload = function () {
  78. loadTasks();
  79. };
  80. </script>
  81. </body>
  82. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement