問題描述
我有一個發送一些數據的 AJAX 請求.數據尊重 multipart/form-data 規范.
I have an AJAX request that sends out some data. The data respects the multipart/form-data specification.
我面臨的問題是瀏覽器將 Content-Type 標頭設置為 text/plain 并且它應該是 multipart/form-data.
The problem I'm facing is that the browser sets the Content-Type header to text/plain and it should be multipart/form-data.
我試過這樣做:request.setRequestHeader("Content-Type", "multipart/form-data");
但這會發出 400 Bad Request 錯誤.
I've tried doing this: request.setRequestHeader("Content-Type", "multipart/form-data");
but this gives out an 400 Bad Request error.
如果我這樣做 request.setRequestHeader("Content-Typexxxx", "multipart/form-data");
沒有錯誤,則設置了Content-Typexxxx"標頭,但顯然是對我沒有幫助.
If I do request.setRequestHeader("Content-Typexxxx", "multipart/form-data");
there is no error, the "Content-Typexxxx" header is set but it obviously is no help to me.
我猜有一個可以設置的有效 Content-Type 標頭列表,multipart/form-data"不在其中,但我找不到解決我的困境的方法.
I guess there is a list of valid Content-Type headers one can set and "multipart/form-data" isn't among them, but I cannot find a sollution to my predicament.
實際發送的數據示例:
Content-Type: multipart/form-data; boundary=l3iPy71otz
--l3iPy71otz
Content-Disposition: form-data; name="titluPublic"
Variation_1
--l3iPy71otz
Content-Disposition: form-data; name="nr_versiune"
--l3iPy71otz--
謝謝!
推薦答案
你沒有在請求頭中設置boundary
,如:
You didn't set the boundary
in your request header, as in:
request.setRequestHeader("Content-Type", "multipart/form-data; boundary=l3iPy71otz");
有關詳細信息,請參閱 RFC 2045:
For more information, see RFC 2045:
5 內容類型標題字段
[…]
參數是媒體的修飾符子類型,因此不要從根本上影響性質內容.有意義的集合參數取決于媒體類型和亞型.大多數參數是與單個特定的相關聯亞型.然而,一個給定的頂級媒體類型可以定義適用的參數該類型的任何子類型.參數可能需要他們的定義內容類型或子類型,或者它們可能是選修的.MIME 實現必須忽略其名稱的任何參數不認識.
5 Content-Type Header Field
[…]
Parameters are modifiers of the media subtype, and as such do not fundamentally affect the nature of the content. The set of meaningful parameters depends on the media type and subtype. Most parameters are associated with a single specific subtype. However, a given top-level media type may define parameters which are applicable to any subtype of that type. Parameters may be required by their defining content type or subtype or they may be optional. MIME implementations must ignore any parameters whose names they do not recognize.
例如,字符集"參數適用于任何子類型文本",而邊界"任何子類型都需要參數多部分"媒體類型.
For example, the "charset" parameter is applicable to any subtype of "text", while the "boundary" parameter is required for any subtype of the "multipart" media type.
更新:我發現的另一個問題 on the net 當 charset
添加到 Content-type 時出現code> 在請求標頭中,但不在正文中的消息邊界中(這也適用于您的測試用例).這似乎不是一個可行的解決方案,但也許會有所幫助.
Update: Another problem I found on the net appears when a charset
is added to the Content-type
in the request header, but not in the message boundaries in the body (this is also true for your test case). It doesn't seem a likely solution, but perhaps it helps.
在您的情況下,將 charset
顯式添加到請求標頭和消息邊界中:
In your case, explicitly add a charset
to both the request header and in the message boundaries:
data.params += "--" + data.uniqid + "; charset=UTF-8" + data.crlf;
…
request.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + data.uniqid + "; charset=UTF-8");
更新 2: 在我自己在本地嘗試后,我注意到前導邊界沒有被識別為這樣,而是被解釋為最后一個參數內容(在我更寬容的服務器上).也許這導致 Apache 拋出 400 Bad Request
錯誤.
Update 2: After trying this myself locally, I noticed the leading boundary wasn't recognized as such, but interpreted as the last parameter contents (on my more forgiving server). Perhaps that was causing Apache to throw a 400 Bad Request
error.
經過反復試驗,我注意到這是因為服務器期望 charset
位于 every 邊界,甚至是最后一個邊界.為了防止混淆,我決定在請求頭 before 邊界參數中顯式設置 charset
,以便邊界將是 Content- 中的最后一個參數類型
請求頭.在此之后,一切似乎都運行良好.
After some trial and error, I noticed that that was caused because the server expected the charset
to be in every boundary, even the last one. To prevent confusion, I decided to explicitly set the charset
in the request header before the boundary parameter, so that the boundary would be the last parameter in the Content-type
request header. After this, everything seemed to work fine.
data.params = "Content-Type: multipart/form-data; boundary=" + data.uniqid;
…
data.params += "--" + data.uniqid + data.crlf;
…
data.params += "--" + data.uniqid + "--";
…
request.setRequestHeader("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + data.uniqid);
這篇關于在 XHR 中使用 multipart/form-data 作為 Content-Type 時收到“400 Bad Request"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!