0

enter image description here

Hi so I'm trying to create a log in form by getting a username and password from the HTML and checking if the username = Name and password = UID from the server response. Then if that is true access should be true and I would like to go to the window location "start.html" but I can't get it to go there. The server works fine. Anyone has an idea of why is this happening?

Update:: If I move the window.location inside the onload function still doesn't work

function connection() {
  var username = document.getElementById("username").value;
  var password = document.getElementById("password").value;
  var request = new XMLHttpRequest();
  request.open("GET", "http://localhost:8080/users?uid=" + password, true);

  request.onload = function () {
    // Success!
    data = JSON.parse(this.response);
    if (data.Nombre == username && data.UID == password) {
      access = true;
    }
    if (access == true) {
      window.location = "start.html";
    }
  };

  request.onerror = function () {
    // There was a connection error of some sort
  };


  request.send();
  • Put `window.location = "start.html";` inside of your onload function's if-statement. Your `if (access == true) {` is currently running _before_ your onload function will run, so `access` will never be true when this if-statement runs – Nick Parsons Nov 22 '20 at 10:04
  • yeah I already tried it but then the server doesn’t get the request – Anna Garcia Nov 22 '20 at 10:19
  • What do you mean by the server doesn't get the request? As in `"http://localhost:8080/users?uid=" + password` doesn't get the uid parameter? Moving `window.location` inside of the if-statement should not change this. (btw, you shouldn't be doing authentication on the client-side, someone can easily disable or modify your JavaScript to bypass this check) – Nick Parsons Nov 22 '20 at 10:24
  • Yeah it's just a code I'm doing to learn about this stuff. But I just tried it like this: request.onload = function () { // Success! data = JSON.parse(this.response); if (data.Nombre == username && data.UID == password) { access = true; } if (access == true) { window.location = "start.html"; } }; – Anna Garcia Nov 22 '20 at 10:25
  • And still won't work – Anna Garcia Nov 22 '20 at 10:26
  • You can simplify that to be just https://jsfiddle.net/oq82crm3/. But what do you see if you try and `console.log(data)` after `data = JSON.parse....`. Also, are you getting any errors in your browser's console? – Nick Parsons Nov 22 '20 at 10:29
  • I am quite a noob and I don't know how to see the console on html javascript files :/ – Anna Garcia Nov 22 '20 at 10:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224920/discussion-between-nick-parsons-and-anna-garcia). – Nick Parsons Nov 22 '20 at 10:32

0 Answers0