Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <!-- Disable JavaScript blockers in browser -->
  6.  
  7. <script>
  8. var xhr;
  9.  
  10. login("http://localhost:8080/login", "user", "userPass");
  11.  
  12. function login(url, username, password) {
  13.   xhr = new XMLHttpRequest();
  14.   xhr.open("POST", url, true);
  15.   xhr.onreadystatechange = onStateChange;
  16.   xhr.onload = onSuccess;
  17.   xhr.onerror = onError;
  18.   xhr.send(getFormData(username, password));
  19. }
  20.  
  21. function onSuccess() {
  22.  var responseText = xhr.responseText;
  23.  console.log('responseText:', responseText);
  24. }
  25.  
  26. function onError() {
  27.   console.log('There was an error!');
  28. }
  29.  
  30. function onStateChange() {
  31.   if (xhr.readyState === 4) {
  32.     console.log('Status:   ' + xhr.status);
  33.     console.log('Response: ' + xhr.responseText);
  34.   }
  35. }
  36.  
  37. function getFormData(username, password) {
  38.   var formData  = new FormData();
  39.   formData.append("username", username);  // "username" must NOT be camel-case!
  40.   formData.append("password", password);  // "password" must NOT be camel-case!
  41.   return formData;
  42. }
  43. </script>
  44.  
  45. </body>
  46. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement