2014-03-06 56 views
0

如果我只是使用序列化的表单作为数据,我可以让它工作,但由于我试图包含上传文件,它也失败了,它什么也不做?苦于使用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> 
+0

您确信您的代码是100%正确的? –

+0

哪个代码?我知道ask_advice_ajax.php是正确的,因为ot与序列化的形式工作,我不相信jquery – tatty27

+0

它是如何失败?服务器有错误吗? JavaScript控制台上的错误?当您将其与工作版本进行比较时,它生成的请求有什么不同? – David

回答

1

无法发送文件只是把FORMDATA要求身体。 我认为这篇文章将有助于你:Ajax file upload

0

得到它使用该工作...

$(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,