views:

741

answers:

2

I have a JQuery/Ajax update. It correctly updates in all major browsers, however, dealing with error responses does not work in any version of IE.

Heres the ajax submit code:

    $('.ajax_multi_submit').click(function(event){
 // if javascript is enabled, stop default post
 event.preventDefault();

 // get the id assigned to this form. This id will
 // be added to every id to used in processing
 var element = $(this);
 var Id = element.attr("id");  
 var formUrl = $('#ajax_multi_form'+Id).attr('action'); 

 $.ajax({
   url: formUrl,
   type: "POST",
   dataType: "html",
   data: $('#ajax_multi_form'+Id).serialize(),
   beforeSend: function(){
    showAjaxMultiBusy(Id);
   },
   complete: function(){
   },
   success: function(html){
    processAjaxMultiForm(html, Id);
   }
  }); 
});

Again, I don't think the problem is here, as all browsers post updates correctly. However, when processing the result with this code:

    function processAjaxMultiForm(html, Id){

 $('#ajax_errors'+Id).hide('slow');

 window.setTimeout( function(){

   $('#ajax_busy'+Id).hide('slow');

   if (parseFloat(html)){
    $('#ajax_message'+Id).html('Record Saved.').show('normal');
    $('#ajax_message'+Id).hide('slow');
   }else{

    $('#ajax_errors'+Id).html(html).show('slow');
   }
 }, 1500);
}

When an error string is returned instead of an integer ( parseFloat(html) = "some string..." ), FF and Safari correctly display the error in the "else" condition.

IE will always display the "if" condition, regardless of return value (1=success, string=error).

I assume some genius out there will see the problem immediately?

EDIT ------- Here's a bit more info. I added a console log to the script to output the html variable. I am getting a proper string on errors, and a "1" on success. I've updated the code per a suggestion below to:

function processAjaxMultiForm(html, Id){

$('#ajax_errors'+Id).hide('slow');

window.setTimeout( function(){

$('#ajax_busy'+Id).hide('slow');

console.log("html is: " +html);

if (isNaN(html)){
 $('#ajax_errors'+Id).html(html).show('slow');

}else{
 $('#ajax_message'+Id).html('Record Saved.').show('normal');
 $('#ajax_message'+Id).hide('slow');
}

}, 1500); }

As before, works as expected in FF Safari, but IE evaluates both string (error) and integer (success) as a "success"

+1  A: 

isNan should work.

Andy Gaskell
Thanks for the try! Didn't work.I swapped in isNAN as suggested (see below). Works fine for safari and FF. Doesn't work in IE8. Haven't tried 6 or 7 yet.IE shows "record saved" regardless of success or not. if (isNaN(html)){ $('#ajax_errors'+Id).html(html).show('slow'); }else{ $('#ajax_message'+Id).html('Record Saved.').show('normal'); $('#ajax_message'+Id).hide('slow'); }
pdxtim
I just tried isNaN("abc") and isNaN("123") in IE with Developer Tools (Tools -> Developer Tools) and I got expected results (true and false respectively)
Andy Gaskell
Thanks for your help. I discovered an error upstream. I don't understand why it would impact a string vs integer evaluation, but the upstream change made this one go away. THANK YOU
pdxtim
+1  A: 

How about trying (isNan(parseFloat(html)).

EDIT:

Alternatively why don't you return "Success" to aid in clarity.

ChaosPandion
Thanks for your help!
pdxtim