I'm currently trying to run a script which works on my LAMP environment but not on a WAMP environment:
$.ajax(
{
url: '<?php echo ROOT_DIR;?>/member/login-process',
type: "post",
data: $('form').serialize(),
success: function(data)
{
if (data == 'success')
{
setTimeout(function(){window.location.href = '<?php echo ROOT_DIR;?>/dashboard';}, 2000);
}
else
{
$("#alert").html('<div class="alert alert-error"><i class="icon-exclamation-sign"></i> '+data+'</div>');
}
}
});
When I try it on LAMP, it works fine: i'm redirected. With WAMP, I don't know why, but I have the following error message (generated by the "else"):
success
I've tried typeof(data) and I have "string" as result. The value returned by the Ajax query is therefore the "success" string, so why the "if" is ignored?
Try this:
var go = $.ajax({
type: 'POST',
url: '<?php echo ROOT_DIR;?>/member/login-process',
data: $('form').serialize()
})
.done(function(data) {
console.debug("DATA:");
console.debug(data);
if (data == 'success')
{
setTimeout(function(){window.location.href = '<?php echo ROOT_DIR;?>/dashboard';}, 2000);
}
else
{
$("#alert").html('<div class="alert alert-error"><i class="icon-exclamation-sign"></i> '+data+'</div>');
}
})
.fail(function(msg) {
alert('Error: ' + msg);
})
.always(function() {
});
success
has been the traditional name of the success callback in jQuery, defined as an option in the ajax call. However, since the implementation of $.Deferred
and more sophisticated callbacks, done
is the preferred way to implement success callbacks, as it can be called on any deferred.