jqueryajaxjquery-forms-plugin

jQuery Ajax form get submitted form as return parameter in case of an error


I'm using the jQuery Form Plugin to generate and submit my Ajax forms. However if a ajax call fails, I would like to offer the option to option to resend the form and therefore built a displayError() function, which will print an the top bar of the website with the error provided and a button to submit the failed form again.

However, as I am using multiple forms with the same function within this website and with the same class obviously, I need to tell the displayError() function which form was just submitted and failed. Within the beforeSubmit or success function I have a so called "myform" parameter which unfortunately is not available within the error function for whatever reason.

Is there any possibility to access the current DOM Object of the form submitted within the error function of the ajax call?

$('.class').ajaxForm({ 
    beforeSubmit: function(data, myForm){

    },
    success: function(responseText, status, xhr, myForm){
        displaySuccess(responseText);
    },
    error: function(xhr, textStatus, errorThrown){
        displayError();
    }
});

This is just a simplified version of the code I am using but should be enough to understand the problem. Thank you very much in advance!


Solution

  • As @sh4dowb pointed out running the ajaxform within a .each function is actually the best solution to solve this problem. Just in case someone is experiencing the same issues, I am answering my own question.

    $('.class').each(function(){
        var that=this;
        $(this).ajaxForm({
            beforeSubmit: function(data, myForm){
    
            },
            success: function(responseText, status, xhr, myForm){
                displaySuccess(responseText);       
            },
            error: function(xhr, textStatus, errorThrown){
                displayError(function(){$(that).submit()});
            }
        });
    });
    

    This actually solved my problem. As just as mentioned above, this is just a simplified version of the complete script again.