I've got the below ajax code and it works fine. If I send a request the first time, it works fine. If I send the second time, It returns success and error (1 each).
$('#eproductForm').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: $('#eproductForm').attr('action'),
data: $('#eproductForm').serialize(),
cache: false,
async: false,
dataType: 'json',
success: function (response) {
$('#eproductModal').modal('hide');
$('#eproductForm')[0].reset();
save_success();
stockList();
},
error: function(response){
something_wrong();
}
});
return false;
});
Do you have a solution please? share. Thanks
Why are you returning false at the end!? I'm hoping
save_success();
stockList();
this two functions will take you to another place, the thing is when you are returning false maybe the submit aint working correctly so it falls down to the error. Try returning true in success/ false in error, maybe this will fix it. If this doesn't work, try making the Ajax request a promise as in
return new Promise ((resolve, reject) => {
$.ajax({
type: 'post',
url: $('#eproductForm').attr('action'),
data: $('#eproductForm').serialize(),
cache: false,
async: false,
dataType: 'json',
success: function (response) {
resolve(response)
},
error: function(response){
reject(err)
}
});
}).then((response) => {
$('#eproductModal').modal('hide');
$('#eproductForm')[0].reset();
save_success();
stockList();}).catch((err) => {something_wrong();})
this will help you understand how the request is passing through your function. Also, it helps to check your rquest in the chrome developer tools and see what status is returning, so you can deduce at what point is breaking