So I have a form that I'm trying to submit via AJAX: This is how I set it up to test my php script that handles the login.
<form class="login_form" method="post" action="login_ajax.php">
Email: <input name="email" type="text" /> <br />
Password: <input name="pass" type="password" /> <br />
<input type="submit" />
</form>
This seems to work; correct credentials give a good return, json object with a "success" attribute, incorrect credentials gives a "failed" json object.
But then when I try to set up the script, nothing works.
$(document).ready(function() {
$(".login_form").submit(function () {
try {
$.post("login_ajax.php", {'email':this.email.value, 'pass':this.pass.value},
function (data) {
alert("Back from AJAX");
}, 'json');
alert("Sent to AJAX");
}
catch(e) {
alert(e);
}
return false;
});
});
I'm not getting any exceptions, but nor am I getting the "BACK" alert. I just get the "Sent to AJAX" and then nothing. Is there something I misunderstand about the callback function passed to jQuery.post, that it would never be called?
EDIT: I replaced my .post line with the one below, and it worked correctly, the success function gets called and I've got the data that I want. I was under the impression that these were essentially equivalent.
$.ajax("login_ajax.php",
{'type':'POST',
'data':{'email':this.email.value, 'pass':this.pass.value},
'error':function(thingy,status,em) {alert("ERROR: "+em);},
'success':function(data, status) {alert(JSON.stringify(data));}
});
jQuery,