mardi 24 février 2015

jQuery ajax form.submit() in .done always triggered

I'm using the following Javascript to check a form before actually submitting.



$('#homesubmit').click(function(e){
e.preventDefault();
if (!$("#inputcode").val()) {
e.preventDefault();
$("#inputcode").qtip({
content: 'Please enter code!.',
style: {
/* Other qtip settings and stuff */
})
}
else {
var jqxhr = $.post( "<?php echo base_url().'ajaxhandler?action=checkcodehome'?>", $( "#startcode" ).serialize())
.done(function() {
alert( "Correct!");
/* // If code is correct, please do submit the form...
$.('#startcode').submit(); */
})
.fail(function() {
alert( "Wrong!");
/* Maken a qtip tooltip*/
});
}
});


Ajaxhandler gives a 200 OK header when the form is ready to be submitted and a 404 when that's not the case. This works wonderful with the alerts that I've got in my example, but when I want to call .submit() on the form on succes the form is submitted on .fail as well.


I guess I could have my ajaxhandler return a json with true/false and check that value in .done, but that seems redundant since there's no data that I want to send.


Why is the alert() triggered that corresponds to the response, but is $.('#startcode').submit() always called?


Kind regards


Aucun commentaire:

Enregistrer un commentaire