Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Initial JS File
  3. */
  4.  
  5. //https://stackoverflow.com/questions/247483/http-get-request-in-javascript
  6. function httpGetAsync(theUrl, callback)
  7. {
  8.     var xmlHttp = new XMLHttpRequest();
  9.     xmlHttp.onreadystatechange = function()
  10.     {
  11.         if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
  12.             //run the function provided as the callbacks
  13.             callback(xmlHttp.responseText);
  14.     }
  15.     xmlHttp.open("GET", theUrl, true); // true for asynchronous request
  16.     xmlHttp.send(null);
  17. }
  18.  
  19.  
  20. function requestCallback(responseText)
  21. {
  22.     /*
  23.     take the response text, and set it to the "serverTime"
  24.     element on the page so the user can see it
  25.     */
  26.     let serverTimePElement = document.getElementById("servertime");
  27.     serverTimePElement.innerHTML = responseText;
  28. }
  29.  
  30. var submit = document.getElementById("submit");
  31. var clear = document.getElementById("clear");
  32. var Test = document.getElementById("Test");
  33. var submitDate = new Date();
  34.  
  35.  
  36. submit.onclick = function buttonAction(){
  37.  
  38.     var title = document.getElementById("title").value;
  39.     var content = document.getElementById("contents").value;
  40.     var payLoad = {
  41.         'Title': title,
  42.         'Contents': content,
  43.         'Submit Date': submitDate
  44.         }
  45.  
  46.     dataPost("/submitNote", payLoad);
  47.  
  48. }
  49.  
  50. function dataPost(url, data){
  51.     var xhr = new XMLHttpRequest();
  52.     var url = '/submitNote';
  53.  
  54.  
  55.     xhr.onreadystatechange = function () {
  56.         if (xhr.readyState === 4 && xhr.status === 200);
  57.         }
  58.  
  59.         xhr.open("POST", url, true);
  60.         xhr.setRequestHeader("Content-Type", "application/json");
  61.         var jsonData;
  62.         if(data != null) jsonData = JSON.stringify(data);
  63.         xhr.send(jsonData);
  64. }
  65.  
  66. Test.onclick = function(){
  67.  
  68.     httpGetAsync("/getFileLists", function(response){
  69.         console.log(response)
  70.     });
  71.  
  72.  
  73. }
  74.  
  75. clear.onclick = function(){
  76.     document.getElementById("title").value = ""
  77.     document.getElementById("contents").value = ""
  78.     console.log("cleared");
  79. }
  80.  
  81.  
  82.  
  83. //submit request to server for the time when the page loads
  84. httpGetAsync("/getServerTime", requestCallback);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement