問題描述
我正在通過 XMLHttpRequest 發(fā)送帖子數(shù)據(jù):
I am sending post data via 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 或其他任何內(nèi)容)數(shù)據(jù),則會出現(xiàn)錯誤:
In Python, I get an error if I try to get the POST (or FILES or anything) data:
MultiPartParserError: Invalid boundary in multipart: None
這永遠(yuǎn)行不通嗎?我真的需要將表單主體創(chuàng)建為單個字符串,在其中循環(huán)遍歷參數(shù)并在每個參數(shù)之前和之后放置一個邊界字符串嗎?如果是這樣,那應(yīng)該是什么樣子?如何從 Python 中的 POST 中獲取它?或者有沒有更簡單的方法.我環(huán)顧四周,并沒有找到太多關(guān)于此的內(nèi)容.
Can this never work?? Do I really need to create the form body as a single string where I loop through the parameters and place a boundary string before and after each one? And, if so, what should that look like? How do I get it from my POST in Python?? Or is there an easier way. I'm looking around and not finding much on this.
順便說一句,我正在使用multipart/form-data",因為我的字符串?dāng)?shù)據(jù)非常長,這是一種更快的發(fā)送方式.當(dāng)我創(chuàng)建表單并將其發(fā)布到 iframe 時,它??對我有用.但是這里我更喜歡xmlHttp.
btw, I am using "multipart/form-data" because my string data is really long and this is a faster way to send it. It has worked for me when I create a form and post it, targeting it to an iframe. But here I much prefer xmlHttp.
推薦答案
不要自己設(shè)置 Content-Type
標(biāo)頭..send()
數(shù)據(jù)時會正確設(shè)置,包括正確生成的邊界,這是您手動生成的標(biāo)題所缺少的.
Do not set the Content-Type
header yourself. It will be properly set when .send()
ing the data, including the proper generated boundary, which your manually generated header lacks.
規(guī)范明確指出 .send(FormData)
將使用 multipart/form-data 編碼.
The spec clearly states that .send(FormData)
will use multipart/form-data encoding.
如果數(shù)據(jù)是 FormData
If data is a FormData
令請求實(shí)體主體為運(yùn)行multipart/form-data編碼算法的結(jié)果,數(shù)據(jù)為表單數(shù)據(jù)集,UTF-8為顯式字符編碼.
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.
設(shè)mime type為multipart/form-data;"、一個U+0020空格字符、boundary="和multipart/form-data編碼算法生成的multipart/form-data邊界字符串的串聯(lián).
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.
這篇關(guān)于XMLHttpRequest multipart/form-data:多部分中的邊界無效的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!