Question

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"

Was it helpful?

Solution

isNan should work.

OTHER TIPS

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

EDIT:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top