submit form with jquery ajax

jQuery -- Posted on Oct. 6, 2023
              
                $('body').on('submit', 'form', function(e){
            e.preventDefault();
            let $form = $(this);
            let url = $(this).attr('action');
            let data = $(this).serialize();
            let method = $(this).attr('method');
            $.when($.ajax({
                url: url,
                data:data,
                method:  method,
                datatype: 'json',
              })).then(function( response, textStatus, jqXHR ) {
              console.info(response);
              $($form)[0].reset();
              var obj = response;
              console.info(obj)
              
            }).catch(function(err){
              $.each(err.responseJSON, function(index, value){
                console.info(index, value);
                $('#id_'+ index).addClass('is-invalid');
                $('#id_'+ index).after("<div class='invalid-feedback'>" + value + "</div>");
            });
            return false
        })
                  
   
            

Related Posts