Events are not triggered in a call to ajax in success

2

As it says in the title I have a call ajax that together I use it with the jquery validator, the problem is when I use the function that comes with the validator that is called isDefaultPrevented () the validation works perfectly without problems except that nothing is activated within the success of the ajax, inside there I have to call a modal and also does not even trigger the console log, if I remove the isDefaultPrevented () and only use the preventDefault, it triggers the verification of the form and detects the errors but it carries out the call ajax and there if the manners work well. Will there be any other alternative? Thank you very much.

$('#candidateForm').validator().on('submit', function(e) {
    if (e.isDefaultPrevented()) {
        // handle the invalid form...
    } else {
        var dataString = $('#candidateForm').serialize();
        $.ajax({
            type: "POST",
            url: "work/php/onlineApplication/addNewCandidate.php",
            data: dataString,
            dataType: "json",
            success: function(result) {
                console.log(result['data'].status);
                if (result['data'].status == 'error') {
                    $("#bodyMsgModal").html(result['data'].message);
                    $("#addUserModal").modal('show');
                    $(result['data'].Field).focus();
                }
                if (result['data'].status == 'success') {
                    $("#bodyMsgModalSuccess").html(result['data'].message);
                    $("#addUserModalSuccess").modal('show');
                    $('#candidateForm')[0].reset();
                }
            }
        });
    }
});
    
asked by Cobol 23.08.2016 в 17:40
source

2 answers

1

This is the part of my solution that works for me

$('#candidateForm').validator().on('submit', function(e) {
    if (e.isDefaultPrevented()) {
        // handle the invalid form...
    } else {
      e.preventDefault();
        var dataString = $('#candidateForm').serialize();
        $.ajax({
            type: "POST",
            url: "work/php/onlineApplication/addNewCandidate.php",
            data: dataString,
            dataType: "json",
            success: function(result) {
                console.log(result['data'].status);
                if (result['data'].status == 'error') {
                    $("#bodyMsgModal").html(result['data'].message);
                    $("#addUserModal").modal('show');
                    $(result['data'].Field).focus();
                }
                if (result['data'].status == 'success') {
                    $("#bodyMsgModalSuccess").html(result['data'].message);
                    $("#addUserModalSuccess").modal('show');
                    $('#candidateForm')[0].reset();
                }
            }
        });
    }
});
    
answered by 23.08.2016 в 19:24
0

And if you try not to make a submit event, but a onclick() ?, that is, to the button you use to send the form, you put an id and change the type of submit to button, I do not know how you have the HTML structure but it should work:

Example

//Aquí el boton
<button type="button" name="enviar" id="enviar">Enviar</button>


$('#enviar').validator().on('click', function(e) {
    e.preventDefault();
        var dataString = $('#candidateForm').serialize();
        $.ajax({
            type: "POST",
            url: "work/php/onlineApplication/addNewCandidate.php",
            data: dataString,
            dataType: "json",
            success: function(result) {
                console.log(result['data'].status);
                if (result['data'].status == 'error') {
                    $("#bodyMsgModal").html(result['data'].message);
                    $("#addUserModal").modal('show');
                    $(result['data'].Field).focus();
                }
                if (result['data'].status == 'success') {
                    $("#bodyMsgModalSuccess").html(result['data'].message);
                    $("#addUserModalSuccess").modal('show');
                    $('#candidateForm')[0].reset();
                }
            }
        });
});
    
answered by 23.08.2016 в 18:50