如何在使用XmlHttpRequest和FormData时设置边界 [英] How to set boundary while using XmlHttpRequest and FormData

查看:241
本文介绍了如何在使用XmlHttpRequest和FormData时设置边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用FormData发布XmlHttpRequest时,我试图在标头中正确设置边界:

I am trying to set the boundary correctly in the header while using FormData to post the 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方法

只需不手动设置Content-Type标头,浏览器就会自动设置"multipart/form-data; boundary = ..."值.

Simply don't set the Content-Type header manually and the browser will automatically set "multipart/form-data; boundary=..." value.

jQuery方法

如果您使用的是jQuery,请将contentType选项设置为false:

If you're using jQuery, set contentType option to false:

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

这篇关于如何在使用XmlHttpRequest和FormData时设置边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆