Hey What am I doing wrong here? Extremely new to ajax.
$(document).ready(function(){
$('#submit').click(function() {
$('#waiting').show(500);
$('#empty').show(500);
$('#reg').hide(0);
$('#message').hide(0);
$.ajax({
type : 'POST',
url : 'logina.php',
dataType : 'json',
data: {
type : $('#typeof').val(),
login : $('#login').val(),
pass : $('#pass').val(),
},
success : function(data){
$('#waiting').hide(500);
$('#empty').show(500);
$('#message').removeClass().addClass((data.error === true) ? 'error' : 'success')
.text(data.msg).show(500)
if(data.error === false)
window.location.replace("http://blahblah.com/usercp.php");
if (data.error === true)
$('#reg').show(500);
$('#empty').hide()
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
$('#waiting').hide(500);
$('#message').removeClass().addClass('error')
.text("There was an Error. Please try again.").show(500);
$('#reg').show(500);
$('#empty').hide();
Recaptcha.reload();
}
});
return false;
});
});
It logs in just fine and gives the success message just fine just no luck on the window.location.replace. How can I fix this?
Try this:
or
you missed some semicolon (will break in IE):
edit: now you use a lot of show/hide events, you want to show a message, and then do the redirect. You should chain your redirect at the end of the show event (so the show animation wait until the end before doing the redirect:
But anyway, other responses certainly contains the main cause of your bug. i.e debug what you have in data.error, it's maybe not false.
It looks like
data.error
is true, even though you ended up in thesuccess
branch. Try putting alerts to clarify your program flow.success : function(data){ alert('success branch'); $('#waiting').hide(500); $('#empty').show(500); $('#message').removeClass().addClass((data.error === true) ? 'error' : 'success') .text(data.msg).show(500) if(data.error === false) alert('no data error'); window.location.replace("http://blahblah.com/usercp.php");
if (data.error === true) alert('data error'); $('#reg').show(500); $('#empty').hide() },