在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到“400 Bad Re

2023-02-15前端开发问题
145

本文介绍了在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到“400 Bad Request"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个发送一些数据的 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455