submitHandler Uncaught SyntaxError: Unexpected token function

1

I am modifying my ajax to use validations from Jquery Validate, I am using this structure and I find a syntax error that I can not find.

function AgregarCliente(){

    // validate and process form here  
    $('#form_cliente').validate({
        rules: {
            // Set rules for special fields (email/phone?)
        },

        // JQuery's awesome submit handler.
        submitHandler(function(form) {

            // Create variables from the form
            var to = $('input#to').val();
            var fullname = $('input#fullname').val(); 
            var emailaddress = $('input#emailaddress').val();  
            var message = $('textarea#message').val();

            // Create variables that will be sent in a URL string to mail.php
            var dataString = 'to=' + to + '&fullname='+ fullname + '&emailaddress=' + emailaddress + '&message=' + message;

            // The AJAX
            $.ajax({  
              type: 'POST',
              url: '/path/to/mail.php',
              data: dataString,
              success(function(data) {

              }),
                error(function(){
                    alert('Whoops! This didn\'t work. Please contact us.')
                });
            });
            return false;
        });
    });

}

The console flags the error in the line "submitHandler (function (form)" Uncaught SyntaxError: Unexpected token function

Thanks greetings!

    
asked by Javier Antonio Aguayo Aguilar 10.04.2017 в 18:58
source

1 answer

1

According to the validate documentation it is as follows:

submitHandler = function(form) {
// Tú código de acciones a realizar
}

however you have:

submitHandler(function(form) 

To avoid these details I suggest armes (let's say) the complete skeleton and then fill in, that is something like:

$("#form_cliente").validate({
  rules : {
  },
  submitHandler: function(form) {
    // Todo lo que va
    $.ajax({  
       type: 'POST',
       url: '/path/to/mail.php',
       data: dataString,
       success:function(data) {

       },
       error : function(){
            // Te sugiero ver la documentación y agregar los demas parámetros que recibe en caso de error
            // lo anterior te permitirá saber más "preciso" que error se reportar y por consiguiente podrás depurar y corregir.
            alert('Whoops! This didn\'t work. Please contact us.');
       }
     });
  }
});

like that for the style and with it ready you begin to fill with the desired functionality.

By the way, in your code I see a return false , will it always return false? Of course you can be using it because of the detail you have. Successes.

Importate In the declaration of error and success in the ajax you have the same syntax error. Remember each instruction to end it in ";" do not omit explicitly indicating each end of instruction.

    
answered by 10.04.2017 в 19:21