在Ajax文件上传中挣扎



如果我只使用序列化表单作为数据,我可以使其工作起作用,但是由于我尝试将文件上传起来,它也会失败,它无能为力?

 $(document).ready(function() {  
    $('#advice_submit').click(function(e){
     var formData = new FormData($('form#ask_advice_form')[0]); 
              $.ajax({
              url: 'ajax/ask_advice_ajax.php',
              type:'POST',
              data: formData,
              dataType: 'json',
                success: function(response){
                    $('#success').html(response.question_id+' '+response.user); 
                }, // End of success function of ajax form
                error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
             }
        }); // End of ajax call 
     });//close whole function
  });//close whole function

表格

    <form method="post" enctype="multipart/form-data" id="ask_advice_form">
         <input type="text" id="desc" name="desc"  maxlength="50">
         <textarea name="advice_question" id="advice_question"></textarea>
         <input type="file" name="file" id="image" style="border:none">
    <input type="button" name="advice_submit" id="advice_submit" value=""
          class="request_opinion white_submit" >
     </form>

使用此...

让它开始工作
$(document).ready(function() { 
$("#advice_submit").click( function() {
  var formData = new FormData($('#ask_advice_form')[0]);
     $.ajax({
           url: 'ajax/ask_advice_ajax.php',
           data: formData,
           async: false,
           contentType: false,
           processData: false,
           cache: false,
           type: 'POST',
           success: function(response)
           {
               alert('smeg');
           },
         });    return false;    
    });
});

似乎我缺少这些元素:

async: false,
contentType: false,
processData: false,
cache: false,

最新更新