久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 XHR 中使用 multipart/form-data 作為 Content-Type 時收

Getting #39;400 Bad Request#39; when using multipart/form-data as Content-Type in XHR(在 XHR 中使用 multipart/form-data 作為 Content-Type 時收到“400 Bad Request)
本文介紹了在 XHR 中使用 multipart/form-data 作為 Content-Type 時收到“400 Bad Request"的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個發送一些數據的 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 日韩国产欧美在线观看 | 久久99精品久久久久久国产越南 | 国产成人自拍一区 | 欧美在线高清 | 亚洲精品免费视频 | 91在线一区二区 | 久久成人免费 | 国产精品久久网 | 99精品久久久 | 日韩不卡视频在线 | 久久精品国产免费看久久精品 | 看av片网站 | 亚洲啊v在线 | 日韩伦理电影免费在线观看 | 在线观看国产精品视频 | 在线中文字幕亚洲 | 亚洲精品视频在线播放 | av资源网站 | 成人一区二区在线 | 欧美日韩国产精品一区 | 国产精品久久久乱弄 | 国产视频91在线 | 日日夜夜天天久久 | 亚洲欧美一区二区三区国产精品 | 亚洲欧美另类在线 | 国产免费一区二区 | 野狼在线社区2017入口 | 性高湖久久久久久久久3小时 | 亚洲人成人一区二区在线观看 | 丁香六月激情 | 亚洲精品在线看 | 午夜欧美| 久久国产三级 | 国产福利在线视频 | 成人国产精品视频 | 91看片网 | 成人激情视频网 | 久久精品女人天堂av | 欧美视频一区二区三区 | 9999国产精品欧美久久久久久 | 中文成人无字幕乱码精品 |