ajax logic is not working correctly
Hello,
I am very beginner in JavaScript and don't know how to troubleshoot this.
The last logic where it says if(response == 0), else....doesn't work. whatever the result of my query is it will return as else.
I found this code from the page below:
http://woork.blogspot.com/2007/10/login-using-ajax-and-coldfusion.html
What I need to do here is if response is not equal to 0 then send user to page window.location='pagexyz.cfm'; and else go to a different page.
Thanks in advanced
<script type="text/javascript" language="JavaScript">
<cfoutput>
/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function login() {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('login_response').innerHTML = "Loading..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var email = encodeURI(document.getElementById('emailLogin').value);
var psw = encodeURI(document.getElementById('pswLogin').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'includes/loginquery.cfm?email='+email+'&psw='+psw+'&nocache = '+nocache);
http.onreadystatechange = loginReply;
http.send(null);
}
function loginReply() {
if(http.readyState == 4){
var response = http.responseText;
if(response == 0){
// if login failsa
document.getElementById('login_response').innerHTML = 'Login failed! Verify user and password';
// else if login is ok show a message: "Welcome + the user name".
} else {
document.getElementById('login_response').innerHTML = response;
window.location='#eqSiteRoot#member/index.cfm';
}
}
}
</cfoutput>
