XMLHttpRequest multipart/form数据:multi中的边界无效

2024-05-15 01:10:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过XMLHttpRequest发送post数据:

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(或文件或任何东西)数据,就会得到一个错误:

MultiPartParserError: Invalid boundary in multipart: None

这永远行不通吗??我真的需要将表单体创建为一个字符串,在其中循环参数并在每个参数前后放置一个边界字符串吗?如果是的话,应该是什么样子?如何从我在Python中的文章中获得它??或者有更简单的方法。我四处看看,没发现什么。

顺便说一下,我使用的是“多部分/表单数据”,因为我的字符串数据非常长,这是一种更快的发送方式。当我创建表单并将其发布到iframe时,它对我很有用。但在这里我更喜欢xmlHttp。


Tags: 数据字符串表单newdata参数vardomain
1条回答
网友
1楼 · 发布于 2024-05-15 01:10:45

不要自己设置Content-Type头。当.send()处理数据时,它将被正确设置,包括您手动生成的头所缺少的正确生成边界。

spec清楚地说明.send(FormData)将使用多部分/表单数据编码。

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.

相关问题 更多 >

    热门问题