I'm trying to make a login using JavaScript which should check the user authentication using a REST service, using a client in java (for testing) it return a string "hej" if it's successful. My HTML code:
<div class="login">
<form method="POST">
<h1>Login</h1>
<input type="text" name="username" id="username" placeholder="Username" required><br><br>
<input type="password" name="password" id="password" placeholder="Password" required><br><br>
<input type="submit" name="submit" id="submit" onclick="UserAction()">
</form>
</div>
<script src="load_user.js"></script>
And heres my javascript code (load_user.js)
var username;
var password;
function UserAction() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://localhost:8080/Footballmanagerrestservice/webresources/login", false);
xhttp.setRequestHeader("", User());
xhttp.send();
var response = JSON.parse(xhttp.responseText);
console.log(response);
if (response == "hej") {
var url = "http://localhost:8080/FM3/spil2.jsp";
$(location).attr('href', url);
}
}
function User(user, pass) {
username = document.getElementById("username").toString();
username = document.getElementById("password").toString();
var UserAndPass = "?username=" + username + "&password=" + password;
return UserAndPass;
}
I want it to change URL when the login is correct, but it just refreshes the page instead.