i have a form with enctype of multipart/form-data
and i am sending file (image) with FormData and ajax but as i see it is not sending. here is code
$(document).ready(()=>{
$('#form').submit((e)=>{
e.preventDefault();
const formData = new FormData();
const title = $('#title').val();
const news = $('#news').val();
formData.append('title', title);
formData.append('news', news);
formData.append('img', $('#form').find('input[type="file"] [name="thumbnail"]:first')[0].files[0]);
$.ajax({
url: '/admin-panel/add-news',
method: 'POST',
data: formData,
processData: false,
contentType: false,
success: (r)=>{
$('#news-container').html(`<div class="news">${r.news}</div>`);
},
error: (xhr,textStatus,error)=>{
try{
const response = JSON.parse(xhr.responseText);
if(response.message){
$('#error').html(`<p class="err">${response.message}</p>`);
}
}catch(error){
$('#error').html(`<p class="err">${xhr.responseText}</p>`);
}
}
})
})
})
but it gives me error
Uncaught TypeError: Cannot read property 'files'
what is problem?
Thank you!