简体   繁体   English

如何在使用XmlHttpRequest和FormData时设置边界

[英]How to set boundary while using XmlHttpRequest and FormData

I am trying to set the boundary correctly in the header while using FormData to post the XmlHttpRequest: 我正在尝试在标头中正确设置边界,同时使用FormData发布XmlHttpRequest:

xhr.open("POST",url);
xhr.setRequestHeader("Content-type","multipart/form-data; boundary=...");

var formData = new FormData();
formData.append("filename", inputId.files[0]);
formData.append(...);

xhr.send(formData);

How do I get the boundary to be set in the request header here. 如何在此处获取请求标头中设置的边界。 I saw the request being set, the boundary is somehow created in the request. 我看到请求被设置,边界以某种方式在请求中创建。 But the server has no idea on how to interpret it. 但服务器不知道如何解释它。

ES method ES方法

Simply don't set the Content-Type header manually and the browser will automatically set "multipart/form-data; boundary=..." value. 只需不要手动设置Content-Type标头,浏览器就会自动设置“multipart / form-data; boundary = ...”值。


jQuery method jQuery方法

If you're using jQuery, set contentType option to false: 如果您使用的是jQuery,请将contentType选项设置为false:

$.ajax({
    url: url,
    type: 'POST',
    data: formData,
    processData: false,
    contentType: false
});

Try looking at this, How to send multipart/form-data form content by ajax (no jquery)? 试着看看如何通过ajax(没有jquery)发送multipart / form-data表单内容? I am trying to work with this script with PHP as the reciever, having some problems with mixed results of warnings and I think my problem is that I have hacked away at the scripts both ends too much that its no longer functioning. 我正在尝试使用这个脚本与PHP作为接收器,有一些混乱的警告结果的问题,我认为我的问题是我已经砍掉脚本两端太多,它不再运行。

As for the comment by the other poster "If you're using JQuery", the only thing I have to say to that comment is that it is not helpful to the person not working in JQuery and JQ is not the be all and end all of scripts. 至于另一张海报的评论“如果你正在使用JQuery”,那么我唯一要对该评论说的是,它对没有在JQuery工作的人没有帮助,而JQ并不是全部并且最终都是脚本

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

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