javascript - error callback function is fired even if request is successful -
i have script uses ajax send mail. have tested checking email receive mail , true enough, ajax request successful. checked console window of firefox browser , shows me successful message. problem here instead of done
callback function, error
callback function fired. may wonder why i'm still using error
function instead of fail
. reason because when tried using fail
function, doesn't trigger alertbox have set inside it. did go , use error
function again since @ least triggers alertbox made.
here script:
<script type="text/javascript"> var submitbutton = $('#submit'); // variable cache button element var alertbox1 = $('.success'); // variable cache meter element var alertbox2 = $('.alert'); var closebutton1 = $('.close1'); // variable cache close button element var closebutton2 = $('.close2'); // variable cache close button element $( function(){ $( '#contactform' ).submit( function(e){ e.preventdefault(); console.log( 'hello' ); var formdata = $( ).serialize(); console.log( formdata ); $.ajax({ type: 'post', url: 'send.php', data: formdata, datatype: 'json', done: function(){ $(submitbutton).fadeout(500); // fades out submit button when it's clicked settimeout(function() { // delays next effect $(alertbox1).fadein(500); // fades in success alert }, 500); }, error: function(){ $(submitbutton).fadeout(500); // fades out submit button when it's clicked settimeout(function() { // delays next effect $(alertbox2).fadein(500); // fades in fail alert }, 500); } }); }); $(closebutton1).click(function() { // initiates reset function $(alertbox1).fadeout(500); // fades out success message settimeout(function() { // delays next effect $('input, textarea').not('input[type=submit]').val(''); // resets input fields $(submitbutton).fadein(500); // fades in submit button }, 500); return false; // stops success alert being removed want hide }); $(closebutton2).click(function() { // initiates reset function $(alertbox2).fadeout(500); // fades out success message settimeout(function() { // delays next effect $('input, textarea').not('input[type=submit]').val(''); // resets input fields $(submitbutton).fadein(500); // fades in submit button }, 500); return false; // stops fail alert being removed want hide }); }); </script>
what seems 1 causing this? reiterate, i've tried using fail
instead of error
callback function since 1 of answers found in internet , because know fact error
function deprecated. because of reason mentioned above, i've no choice use it.
if refer documentation, cannot use done inside ajax function callback. either use success or add done @ end of ajax call.
$.ajax({ // url, data etc success: function() { //success handler }, error:function(){ //error handler } }); (or) $.ajax({ // ajax related codes }).done(function(){ //callback });
also if aren't returning json server, remove datatype: 'json',
ajax call.
Comments
Post a Comment