【问题标题】:Ajax POST request is emptyAjax POST 请求为空
【发布时间】:2013-08-09 12:45:13
【问题描述】:

我尝试使用 Ajax 上传文件。但是 POST 请求是空的。

var fd = new FormData();
fd.append('file', files[0].name);
alert(files[0].name);
$("#form_upload").submit();
$.ajax({
    method: "POST",
    url: "/dropupload/",
    contentType: false,
    processData: false,
    data: fd,
    success: function(data) {
        waitforprocess();
    },
    complete: function(data){
        alert(formdata.size)
    },
    error: function(){
        allert("error")
    }
}); 

在视图中 POST 是空的

def post(request):
    q = request.POST.get("file")

q = 无

为什么 POST 是空的?

【问题讨论】:

  • 请注意,FormData() 并非所有/旧版浏览器都支持 - 例如,IE 需要 v10+,而后者至少需要 Windows 7。
  • 是你的表单 multipart 添加 enctype='multipart/form-data' 如果不是。

标签: jquery python ajax django post


【解决方案1】:

尝试序列化表单数据,而不是创建新的 FormData 对象。喜欢:

var $form = $("#form_upload").find('form');
var fd = $form.serialize();
// ...

【讨论】:

    【解决方案2】:

    尝试以下方法:

    var fd = new FormData($("#form")[0]);
    

    另外,请确保您的表单声明正确:

    <form id="form" enctype="multipart/form-data">                        
        <input type="file" name="file" />
    </form>
    

    【讨论】:

      猜你喜欢
      • 2013-05-13
      • 2020-07-16
      • 2022-06-14
      • 2016-05-20
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多