Advertisement
Guest User

Untitled

a guest
May 27th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>
  6. Form
  7. </title>
  8.  
  9. <script>
  10. /*****************************************************************/
  11. /* Function that performs (asynchronous) query to the web server */
  12. /*****************************************************************/
  13. function requestAJAX() {
  14. // Create an object representing the request to the web server - see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
  15. var xhr = new XMLHttpRequest();
  16.  
  17. // Registration of a (user) function that will process the response received from the server
  18. xhr.onreadystatechange = () => response(xhr);
  19.  
  20. var input = document.getElementsByName('imie')[0].value;
  21. var uri = 'submit?imie='+input;
  22.  
  23.  
  24. // Execution of the (asynchronous) query to the web server
  25. // xhr.open('GET', uri, true);
  26. // xhr.send();
  27. xhr.open("POST", uri, true);
  28. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  29. xhr.send('imie=' + input);
  30.  
  31. // Examples of the use of the above methods - see https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp
  32. }
  33.  
  34. /************************************************************/
  35. /* The function that processes the response from the server */
  36. /************************************************************/
  37. function response(xhr) {
  38. try {
  39. if (xhr.readyState == XMLHttpRequest.DONE) { // If the response is ready
  40. if (xhr.status == 200) { // If requst was correct
  41. var input = document.getElementsByName('imie')[0].value;
  42. // If the data you receive is a plain text or a JSON document, use the following code
  43. var received_data = xhr.responseText; // Get a response in the form of a string
  44. window.alert(received_data); // and display it
  45.  
  46. // If the data you receive is an HTML or XML document, use the following code
  47. //var xmlDoc = xhr.responseXML; //Receive the answer in the form of object 'XmlDocument', which can be accessed using DOM methods - see https://www.w3.org/TR/domcore/
  48. }
  49. else
  50. window.alert('There was a problem with this request.');
  51. }
  52. }
  53. catch (e) {
  54. window.alert('Exception caught: ' + e.description);
  55. }
  56. }
  57.  
  58.  
  59. /*****************************************************************/
  60. /* Function that performs (asynchronous) query to the web server */
  61. /*****************************************************************/
  62. function requestFetchAPI() {
  63. fetch('/submit') // Execution of the (asynchronous) query to the web server - a promise is created
  64. .then(function (response) { // if the promise is fulfilled
  65. if (!response.ok) {
  66. throw Error(response.statusText);
  67. }
  68. window.alert(response.text()); // show the Promise object
  69. })
  70. .catch(function (error) { // if the promise is rejected
  71. window.alert('Looks like there was a problem: \n', error);
  72. });
  73. }
  74.  
  75. /***********************************************/
  76. /* Same as above but using 'async' and 'await' */
  77. /***********************************************/
  78.  
  79. /*
  80. async function requestFetchAPI() {
  81. try {
  82. response = await fetch('/submit'); // Execution of the (asynchronous) query to the web server - a promise is created
  83. // If the promise is fulfilled, then 'response' has a value
  84. if (!response.ok) {
  85. throw Error(response.statusText);
  86. }
  87. }
  88. catch (error) { // if the promise is rejected
  89. window.alert('Looks like there was a problem: \n', error);
  90. }
  91. window.alert(response.text()); //show the Promise object
  92. }
  93. */
  94. </script>
  95. </head>
  96. <body>
  97. <main>
  98. <form method="get"
  99. action="/submit">
  100. <label>Perform a query to the web server</label> <input type="submit"
  101. value="Without using AJAX"> <input type="button"
  102. value="Using AJAX"
  103. onclick="requestAJAX()"> <input type="button"
  104. value="Using Fetch API"
  105. onclick="requestFetchAPI()">
  106. <input type ="text" name = "imie">
  107. </form>
  108. <p type = 'imie'>
  109.  
  110. </p>
  111. </main>
  112. </body>
  113. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement