Advertisement
Guest User

frontend.js

a guest
Apr 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var login = document.getElementById("login");
  2. var modal = document.getElementById('myModal');
  3. var btn = document.getElementById("signup");
  4. var signUp = document.getElementById("signUpTwo");
  5.  
  6. const PORT = 5001;
  7. const API_BASE_URL = `http://localhost${PORT ? ":" + PORT : ""}`;
  8.  
  9. window.onload = function(){
  10.   const xhr = new XMLHttpRequest;
  11.   xhr.withCredentials = true;
  12.   const url = `${API_BASE_URL}/stuff`;
  13.   // We needed to add a callback function to handle the response!
  14.   xhr.onreadystatechange = function () {
  15.     if (this.readyState == 4 && this.status == 200) {
  16.       console.log(xhr.responseText);
  17.     }
  18.   };
  19.   xhr.open("GET",url,true);
  20.   xhr.send();
  21. }
  22.  
  23. login.addEventListener("click",function(){
  24.     var name = document.getElementById("username").value;
  25.     var pass = document.getElementById("password").value;
  26.     const xhr = new XMLHttpRequest;
  27.     xhr.withCredentials = true;
  28.     const url = `${API_BASE_URL}/check?username=${name}&password=${pass}`;
  29.     xhr.onreadystatechange = function () {
  30.       if (this.readyState == 4 && this.status == 200) {
  31.         console.log(xhr.responseText);
  32.       }
  33.     };
  34.     xhr.open("POST",url,true);
  35.     xhr.send();
  36. })
  37.  
  38. signUp.addEventListener("click",function(){
  39.   var username = document.getElementById("signUpUsername").value;
  40.   var email = document.getElementById("signUpEmail").value;
  41.   var password = document.getElementById("signUpPassword").value;
  42.   console.log(username + " " + password + " " + email);
  43.   const xhr = new XMLHttpRequest;
  44.   const url = `${API_BASE_URL}/signUp`;
  45.   xhr.onreadystatechange = function () {
  46.     if (this.readyState == 4 && this.status == 200) {
  47.       console.log(xhr.responseText);
  48.     }
  49.   };
  50.   xhr.open("POST",url,true);
  51.   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  52.   xhr.send("username="+username+"&password="+password+"&email="+email);//"username="+username+"&password="+password+"&email="+email
  53. })
  54.  
  55. btn.onclick = function() {
  56.     modal.style.display = "block";
  57.   }
  58.  
  59. window.onclick = function(event) {
  60.     if (event.target == modal) {
  61.       modal.style.display = "none";
  62.     }
  63.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement