Hey,
I'm using ajax object configuration for upload type of input. Everything works great, I just have an issue where if validation of uploaded image is rejected, server returns status code 422, so datatables displays "A server error occurred while uploading the file".
For other ajax requests I was able to handle this inside "error" function, but in this case it doesn't seem to be fired. Bellow is my code:
{
label: 'Image:',
name: IMAGE_NAME,
ajax: {
url: route('dt.post'),
type: 'POST',
data: (data) => {
data._token = window.Laravel.csrfToken;
},
dataFilter: function (response) {
const json = JSON.parse(response);
$.each(json.files, function ( table, files ) {
$.fn.dataTable.Editor.files[table] = files;
});
return response;
},
error: function (xhr, e/*, thrown*/) {
let data = xhr.responseJSON;
if (data && data.hasOwnProperty('fieldErrors')){
// success(data);
} else {
// error(xhr, e, thrown);
}
}
},
type: 'upload',
display: function ( file_id ) {
...
},
clearText: "Clear",
noImageText: '-'
}
Any idea why error function is not fired when server code != 200?
Beside this approach I also tried to use ajax function instead of object function(method, url, data, success, error)
(I use this as default method all over the project) so that I was able to call success or error callback, depending on what happened. If I add this to my upload field, upload field will completely ignore function and just make a POST request to the same page user is currently on. This works for DataTables data calls though, so not sure why it doesn't here...
Is there a way to handle validation errors and prevent "A server error occurred while uploading the file" getting displayed for upload field?
P.S.: After using DataTables for 5+ years I think, I think I could easily say it's one of the most advanced and flexible packages I ever used. GJ Allan and team