是否可以为新的FormData(XHR2)对象设置accept-charset或解决方法 [英] Is it possible to set accept-charset for new FormData (XHR2) object or workaround

查看:288
本文介绍了是否可以为新的FormData(XHR2)对象设置accept-charset或解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是示例代码( http://jsfiddle.net/epsSZ/1/):

Here is example code (http://jsfiddle.net/epsSZ/1/):

HTML:

<form enctype="multipart/form-data" action="/echo/html" method="post" name="fileinfo" accept-charset="windows-1251">
  <label>Label:</label>
  <input type="text" name="label" size="12" maxlength="32" value="får løbende" /><br />
  <input type="submit" value="Send standart">
</form>
<button onclick="sendForm()">Send ajax!</button>

JS:

window.sendForm = function() {
  var oOutput = document.getElementById("output"),
     oData = new FormData(document.forms.namedItem("fileinfo"));
  var oReq = new XMLHttpRequest();
  oReq.open("POST", "/echo/html", true);
  oReq.send(oData);
}

当我通过标准格式提交以这种旧方式提交时,请求有效负载如下所示:

When i submit this old way via standart form submit, then request payload looks like this:

------WebKitFormBoundary2890GbzEKCmB08rz
Content-Disposition: form-data; name="label"

f&#229;r l&#248;bende

但是当我以这种方式提交AJAX时,它看起来几乎没有什么不同:

But when i submit this AJAX way, then it looks little different:

------WebKitFormBoundaryPO2mPRFKj3zsKVM5
Content-Disposition: form-data; name="label"

får løbende

如您所见,在前一种情况下,有些字符被字符实体代替, 但是在使用FormData的情况下,存在纯字符串,这当然很好,因为它是utf-8,但是是否有可能使其表现得像标准格式提交?

As you can see, in former case there is some chars is replaced with character entities, but in case of using FormData there is plain string, which is of course good because it's utf-8, but is there any possibility to make it behave like standart form submit ?

推荐答案

您的问题的答案为.您无法更改.根据 XMLHttpRequest2 TR FormData构造的数据是明确的编码为UTF-8.没有提及允许更改它.

The answer to your question is No. You cannot change it. According to XMLHttpRequest2 TR, FormData constructed data is explicitly encoded to UTF-8. With no mention of allowing to change it.

通常的mimeType或Content-Type = charset对于多部分请求无效,因为出于完全相同的原因对其进行了不同的处理.

The usual mimeType or Content-Type=charset become invalid for multi-part requests, since it is handled differently for the exact same reason.

要报价

如果数据是FormData 假设请求实体为运行multipart/form-data编码算法的结果,该算法将数据作为表单数据集,并以 UTF-8作为显式字符编码.

If data is a FormData 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类型为"multipart/form-data;",U + 0020 SPACE字符"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.

希望这会有所帮助!

更新

如果您愿意放弃

new FormData(document.forms.namedItem("fileinfo"));

new FormData().append("name", "value")

可能有可行的解决方案.让我知道您是否正在寻找.

there might be a workable solution possible. Let me know if thats what you are looking for.

另一个更新

有点跑来跑去. 在所有模式下更新小提琴

这就是故事,

1 form with accept-charset="utf8" =>默认行为

内容不需要任何其他转义/编码.因此,该请求会以完整的får løbende

The content does not require any additional escaping/encoding. So the request fires with the text intact as får løbende

2 form with accept-charset="windows-1251" =>您的情况

由于浏览器的默认字符集为utf8,因此内容需要进行其他转义/编码.因此,该内容被转义,然后被解雇,即,发送的内容为f&#229;r l&#248;bende

The content requires additional escaping/encoding, since the default charset of the browser here is utf8. So the content is escaped, and then fired, i.e. the content sent is f&#229;r l&#248;bende

3 FormData constructed with form element

该内容不需要任何其他转义/编码,因为它默认为utf8.因此,该请求将以får løbende文本触发.

The content does not require any additional escaping/encoding, since it defaults to utf8. So the request fires with text as får løbende.

4 FormData constructed, and then appended with escaped data

内容仍然是utf8编码,但是在追加到表单数据之前调用escape(content)并没有什么坏处.这意味着请求将以f%E5r%20l%F8bende文本触发.还是没有骰子吧?

The content is still in the utf8 encoding, but it doesn't hurt to call escape(content) before appending to the form data. This means the request fires with text as f%E5r%20l%F8bende. Still no dice right?

我错了,不.仔细看[read =>凝视几分钟....]

I was wrong, nope. Looking closer[read => staring for a few minutes....] at

f&#229;r l&#248;bende

f%E5r%20l%F8bende

然后所有内容就位-%E5(十六进制)= &#229;(十进制).因此,基本上escape()是Javascript的处理方式,即基于%的编码,它不是HTML友好的.

Then it all fell into place - %E5 (Hexadecimal) = &#229; (Decimal). So basically escape()is Javascript's way of doing things, the % based encoding, which is not HTML friendly.

类似地,我们知道HTML的编码方式是&#;.因此,我为ajax设置了另一种模式,[我想这就是您正在寻找的]

Similarly &#;, as we know is HTML's way of encoding. So I put another mode to ajax, [which is what you are looking for, I'm guessing]

5 FormData constructed, and then appended with html-escaped data

内容仍为utf8编码.使用 stackoverflow 中的这段精彩代码,就像HTML编码一样逃脱它.瞧,该请求以文本f&#229;r l&#248;bende

The content is still in utf8 encoding. Doesn't hurt to escape it like HTML encoding, using this wonderful piece of code from stackoverflow. And voila, the request fired with the text f&#229;r l&#248;bende

在所有模式下更新小提琴

希望这有助于清除它!

更新为Windows-1251全面支持

привет får løbende输入在早期模式5中失败.更新小提琴 http://jsfiddle.net/epsSZ/6/.

This привет får løbende input was failing in earlier mode 5. Update fiddle http://jsfiddle.net/epsSZ/6/.

在此处> https://stackoverflow.com/a/2711936/1304559 和我的解决方案结合使用.因此,问题在于逃避一切.因此,现在仅转义Windows-1251字符集中不存在的字符.

Uses a combination of solution here https://stackoverflow.com/a/2711936/1304559 and mine. So the problem is escaping everything. So now escaping only characters not present in the windows-1251 charset.

这对我希望有帮助!

这篇关于是否可以为新的FormData(XHR2)对象设置accept-charset或解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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