I'm struggling to redirect a user after their login request to the server is successful. The response from the server is successful and I am able to display the values in alert boxes but I don't get redirected to a different page.
Here is the code I am using
<script>
require(["dojo/dom", "dojo/on", "dojo/request", "dojo/dom-form", "dojo/domReady!", "dojo/json"],
function(dom, on, request, domForm, JSON, cookie){
var resultDiv = dom.byId("resultDiv");
on(dom.byId("signinButton"), "click", function(evt){
var emailAddressVar = document.getElementById('emailInput');
var passwordVar = document.getElementById('passwordInput');
if(emailAddressVar != "" && passwordVar != ""){
request.post("http://xxx.xxx.xx.xx:8080/com.thinkingpa.restful.web/SecurityResourceService/loginUser",{
data: {
emailAddress:emailAddressVar.value,
password:passwordVar.value
}
}).then(function(response){
var jsonResponse = "(" + response + ")";
var jsonObject = eval(jsonResponse);
for(var value in jsonObject){
alert(jsonObject[value]);
}
alert( "Your username is: " + jsonObject.username);
if(jsonObject.successful == "true"){
window.location = "http://www.mysite.com";
}else if(jsonObject.successful == "false"){
resultDiv.innerHTML = "<div class=\"info-box\"> <p> The Email or Password you entered was incorrect. </p><p> <a href=\"sign-up.html\">Register</a> to get an account. </p> <div>";
}
resultDiv.innerHTML = "<div class=\"error\">" + response + "<div>";
});
}else{
resultDiv.innerHTML = "<div class=\"info-box\"> <p> Please Fill Out All The Required Fields </p> <div>";
}
});
}
);
</script>
True & false are not strings. Try this:
if(jsonObject.successful == true)
window.location = "http://www.mysite.com";
else
resultDiv.innerHTML = "<div class=\"info-box\"> <p> The Email or Password you entered was incorrect. </p><p> <a href=\"sign-up.html\">Register</a> to get an account. </p> <div>";
Failing that, do alert(jsonObject.successful + ' = ' + typeof(jsonObject.successful))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.