简体   繁体   English

AJAX 错误:“内容类型中没有多部分边界参数”

[英]AJAX error: "No multipart boundary param in Content-Type"

I am trying to upload a file from localhost to a server but I am getting the following error in my network console, with status code 500:我正在尝试将文件从本地主机上传到服务器,但在我的网络控制台中出现以下错误,状态代码为 500:

no multipart boundary param in Content-Type Content-Type 中没有多部分边界参数

I have enabled cors on my nginx reverse proxy.我在我的 nginx 反向代理上启用了cors My AJAX request is like this:我的 AJAX 请求是这样的:

var files = document.getElementById('candidatePhoto').files;
if (!files.length) {
  return alert('Please choose a file to upload first.');
}
var file = files[0];

var form = new FormData();
form.append("files", file);

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://example.com",
  "method": "POST",
  "headers": {
    "Content-Type": "multipart/form-data"
  },
  "processData": false,
  "contentType": false,
  "data": form,
  success: function(data) {
    console.log("Success", data);
    addPhoto(data);
  },
  error: function(err) {
    console.log("Error", err);
    alert("Error: " + err);
  }
}

My HTML form also has enctype='multipart/form-data'我的 HTML 表单也有enctype='multipart/form-data'

If you set contentType: false jQuery will automatically apply the correct Content-Type header for sending multipart data.如果您设置contentType: false jQuery 将自动应用正确的 Content-Type 标头来发送多部分数据。 Your inclusion of a header object explicitly specifying the content type is breaking this behaviour.您包含明确指定内容类型的header对象正在破坏这种行为。 It needs to be removed.它需要被移除。 Change your AJAX request settings to this:将您的 AJAX 请求设置更改为:

var settings = {
  async: true,
  crossDomain: true,
  url: "http://example.com",
  method: "POST",
  processData: false,
  contentType: false,
  data: form,
  success: function(data){
    console.log("Success", data);
    addPhoto(data);
  },
  error: function(err) { 
    console.log("Error", err);
    alert("Error: " + err);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM