Django ajax post form
<p>The code you provided is a JavaScript/jQuery script that handles form submission and AJAX requests. It seems to be a client-side script that interacts with a server to handle form …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | $('body').on('submit', 'form', function(e){
e.preventDefault();
$('input, select').removeClass('is-invalid');
$( ".invalid-feedback" ).remove();
var $form = $(this);
var data = $form.serialize();
var url_link = $form.attr('action');
$.when($.ajax({
url: url_link,
data:data,
method: $form.attr('method'),
datatype: 'json',
})).then(function( response, textStatus, jqXHR ) {
var obj = JSON.parse(response);
console.info(obj)
$('form')[0].reset();
var app = obj[0].model.split('.')[0];
var model = obj[0].model.split('.')[1];
console.info("app:" + app);
console.info("model:" + model);
$("[id$='"+ model + "-list']").prepend("<li>" + obj[0].fields['name'] + "</li>");
}).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>");
});
});
})
|