获得“400错误的请求”使用多/表单数据内容类型的XHR时 [英] Getting '400 Bad Request' when using multipart/form-data as Content-Type in XHR

查看:1240
本文介绍了获得“400错误的请求”使用多/表单数据内容类型的XHR时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送了一些数据的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的,它应该是多部分/表单数据。

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(内容类型,多部分/表单数据); 但是这给了一个400错误的请求错误。

I've tried doing this: request.setRequestHeader("Content-Type", "multipart/form-data"); but this gives out an 400 Bad Request error.

如果我这样做 request.setRequestHeader(内容Typexxxx,多部分/表单数据); 没有错误,内容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头可以设置和多部分/表单数据是不在其中的名单,但我不能找到一个sollution我的predicament。

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--

谢谢!

推荐答案

您没有设置边界在你的请求头,如:

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的Content-Type头字段
[...]
  参数是媒体的改性剂   亚型,并因此不   从根本上影响的性质   内容。该组的有意义   参数依赖于媒体类型   和子类型。大多数参数   与单一特定关联   子类型。然而,给定的   顶级媒体类型可限定   这适用于参数   该类型的任何亚型。参数   可能需要通过它们的限定   内容类型或亚型或者它们可以是   可选。 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.

更新:还有一个问题,我发现<一href="http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/908d1f25-0b15-4b87-a711-6a667e5fad3d/#436f2bdd-09d3-4adc-833d-3b6fc7000c4d"相对=nofollow>网上出现时,字符集添加到内容类型在请求报头,而不是在体内的消息边界(这同样适用于测试案例)。这似乎不是一个可能的解决方案,但也许会有所帮助。

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.

在你的情况,明确添加字符集这两个请求头和消息边界:

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:本地后尝试此我自己,我注意到了领先的边界没有被承认的,但相互preTED作为最后一个参数的内容(我更宽容的服务器上)。也许这是导致Apache将抛出一个 400错误的请求错误。

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.

一些试验和错误之后,我发现这是造成的,因为服务器预计字符集将在的边界,连最后一。为prevent混乱,我决定明确设置字符集在请求头的的边界参数,从而使边界将是在内容类型请求头最后一个参数。在此之后,一切似乎都正常工作。

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);

这篇关于获得“400错误的请求”使用多/表单数据内容类型的XHR时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆