I am currently handling forms by binding submit event and then calling the ajax()
function:
// Code to handle add post form
$('#add_post').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: "add",
dataType: 'json',
data: $(this).serialize(),
success: function (data) {},
error: function (data) {}
})
// Code to handle update post form
$('#edit_post').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: "edit",
dataType: 'json',
data: $(this).serialize(),
success: function (data) {},
error: function (data) {}
})
})
As you can see I have a lot repeating code. How can I refactor my code?