weixin_33690963 2019-08-25 18:13 采纳率: 0%
浏览 227

Ajax没有发送FormData

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!

  • 写回答

1条回答 默认 最新

  • ??yy 2019-08-25 18:18
    关注

    Check your ajax property - contentType

    评论

报告相同问题?