問題描述
我正在通過 XMLHttpRequest 發送帖子數據:
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 或其他任何內容)數據,則會出現錯誤:
In Python, I get an error if I try to get the POST (or FILES or anything) data:
MultiPartParserError: Invalid boundary in multipart: None
這永遠行不通嗎?我真的需要將表單主體創建為單個字符串,在其中循環遍歷參數并在每個參數之前和之后放置一個邊界字符串嗎?如果是這樣,那應該是什么樣子?如何從 Python 中的 POST 中獲取它?或者有沒有更簡單的方法.我環顧四周,并沒有找到太多關于此的內容.
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",因為我的字符串數據非常長,這是一種更快的發送方式.當我創建表單并將其發布到 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.
推薦答案
不要自己設置 Content-Type
標頭..send()
數據時會正確設置,包括正確生成的邊界,這是您手動生成的標題所缺少的.
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.
規范明確指出 .send(FormData)
將使用 multipart/form-data 編碼.
The spec clearly states that .send(FormData)
will use multipart/form-data encoding.
如果數據是 FormData
If data is a FormData
令請求實體主體為運行multipart/form-data編碼算法的結果,數據為表單數據集,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.
設mime type為multipart/form-data;"、一個U+0020空格字符、boundary="和multipart/form-data編碼算法生成的multipart/form-data邊界字符串的串聯.
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.
這篇關于XMLHttpRequest multipart/form-data:多部分中的邊界無效的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!