微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – XMLHttpRequest multipart / form-data:多部分中的边界无效

我通过 XMLHttpRequest发送帖子数据:
var xmlHttp=new XMLHttpRequest();
xmlHttp.open("POST",domain,true);
xmlHttp.setRequestHeader("Content-type","multipart/form-data");
var formData = new FormData();  
formData.append("data",data_json_string);
xmlHttp.send(formData);

在Python中,如果我尝试获取POST(或FILES或任何)数据,我会收到错误

MultiPartParserError: Invalid boundary in multipart: None

这可能永远不会工作?我是否真的需要将表单体创建为单个字符串,我在其中循环参数并在每个参数之前和之后放置一个边界字符串?如果是这样,那应该是什么样的?如何从我的POST中获取它?或者有一种更简单的方法.我环顾四周,对此没有太多了解.

顺便说一句,我正在使用“multipart / form-data”,因为我的字符串数据非常长,这是一种更快的发送方式.当我创建表单并发布它,将其定位到iframe时,它对我有用.但在这里我更喜欢xmlHttp.

解决方法

不要自己设置Content-Type标头.它将在.send()数据时正确设置,包括手动生成的标头缺少的正确生成的边界.

spec明确指出.send(FormData)将使用multipart / form-data编码.

If data is a FormData

Let the request entity body be the result of running the multipart/form-data encoding algorithm with data as form data set and with UTF-8 as the explicit character encoding.

Let mime type be the concatenation of “multipart/form-data;”,a U+0020 SPACE character,“boundary=”,and the multipart/form-data boundary string generated by the multipart/form-data encoding algorithm.

原文地址:https://www.jb51.cc/js/158650.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐