简体   繁体   English

是否可以为新的FormData(XHR2)对象或变通方法设置accept-charset

[英]Is it possible to set accept-charset for new FormData (XHR2) object or workaround

Here is example code ( http://jsfiddle.net/epsSZ/1/ ): 这是示例代码( http://jsfiddle.net/epsSZ/1/ ):

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

But when i submit this AJAX way, then it looks little different: 但是当我提交这种AJAX方式时,它看起来有点不同:

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

får løbende

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 ? 正如你所看到的,在前一种情况下有一些字符被字符实体替换,但是在使用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 . 根据XMLHttpRequest2 TRFormData构造的数据被显式编码为UTF-8 With no mention of allowing to change it. 没有提到允许改变它。

The usual mimeType or Content-Type=charset become invalid for multi-part requests, since it is handled differently for the exact same reason. 通常的mimeType或Content-Type = charset对于多部分请求变得无效,因为出于完全相同的原因,它的处理方式不同。

To quote, 报价,

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 . 如果数据是FormData让请求实体主体是运行multipart / form-data编码算法的结果,其中数据作为表单数据集并且使用UTF-8作为显式字符编码

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. 令mime类型为“multipart / form-data;”的串联,U + 0020 SPACE字符,“boundary =”,以及由multipart / form-data编码算法生成的multipart / form-data边界字符串。

Hope this helps! 希望这可以帮助!

Update 更新

If you are willing to forgo 如果你愿意放弃

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

for 对于

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

there might be a workable solution possible. 可能有可行的解决方案。 Let me know if thats what you are looking for. 如果那就是你想要的,请告诉我。

Another Update 另一个更新

Did a little bit of running around. 有点乱跑了。 Updated fiddle with all modes 更新了所有模式

So this is the story, 所以这是故事,

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

The content does not require any additional escaping/encoding. 内容不需要任何额外的转义/编码。 So the request fires with the text intact as får løbende 因此请求将文本完整地发送为får løbende

2 form with accept-charset="windows-1251" => your case 2 form with accept-charset="windows-1251" =>你的情况

The content requires additional escaping/encoding, since the default charset of the browser here is utf8. 内容需要额外的转义/编码,因为这里的浏览器的默认字符集是utf8。 So the content is escaped, and then fired, ie the content sent is f&#229;r l&#248;bende 所以内容被转义,然后被解雇,即发送的内容是f&#229;r l&#248;bende

3 FormData constructed with form element 3 FormData constructed with form element

The content does not require any additional escaping/encoding, since it defaults to utf8 . 内容不需要任何额外的转义/编码,因为它默认为utf8 So the request fires with text as får løbende . 所以请求以får løbende

4 FormData constructed, and then appended with escaped data 4 FormData constructed, and then appended with escaped data

The content is still in the utf8 encoding, but it doesn't hurt to call escape(content) before appending to the form data. 内容仍然是utf8编码,但在附加到表单数据之前调用escape(content)并没有什么坏处。 This means the request fires with text as f%E5r%20l%F8bende . 这意味着请求使用文本触发为f%E5r%20l%F8bende Still no dice right? 仍然没有骰子对吗?

I was wrong, nope. 我错了,不。 Looking closer[read => staring for a few minutes....] at 仔细观察[读=>盯着几分钟....]

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

f%E5r%20l%F8bende

Then it all fell into place - %E5 (Hexadecimal) = &#229; 然后它全部落到了位置 - %E5 (十六进制)= &#229; (Decimal). (十进制)。 So basically escape() is Javascript's way of doing things, the % based encoding, which is not HTML friendly. 所以基本上escape()是Javascript的做事方式,基于%的编码,这不是HTML友好的。

Similarly &#; 同样&#; , as we know is HTML's way of encoding. ,正如我们所知,是HTML的编码方式。 So I put another mode to ajax, [which is what you are looking for, I'm guessing] 所以我把另一种模式放到了ajax,[这就是你要找的,我猜]

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

The content is still in utf8 encoding. 内容仍然是utf8编码。 Doesn't hurt to escape it like HTML encoding, using this wonderful piece of code from stackoverflow . 使用stackoverflow中的这段精彩代码,像HTML编码一样逃避它是不会有害的 And voila, the request fired with the text f&#229;r l&#248;bende 瞧,请求用文本f&#229;r l&#248;bende解雇了

Updated fiddle with all modes 更新了所有模式

Hope this helps clear it out! 希望这有助于清除它!

UPDATE for windows-1251 full support 更新windows-1251全面支持

This привет får løbende input was failing in earlier mode 5. Update fiddle http://jsfiddle.net/epsSZ/6/ . 这个привет får løbende输入在早期模式中失败了5.更新小提琴http://jsfiddle.net/epsSZ/6/

Uses a combination of solution here https://stackoverflow.com/a/2711936/1304559 and mine. 在这里使用https://stackoverflow.com/a/2711936/1304559和我的解决方案组合。 So the problem is escaping everything. 所以问题在于逃避一切。 So now escaping only characters not present in the windows-1251 charset. 所以现在只转义windows-1251字符集中没有的字符。

This helps it I hope! 这有助于我希望!

Thank you for this question, I enjoyed myself! 谢谢你这个问题,我很开心! :) :)
Replace 更换

<form enctype="multipart/form-data" action="/echo/html" method="post" name="fileinfo" accept-charset="windows-1251">

by 通过

<form enctype="multipart/form-data" action="/echo/html" method="post" name="fileinfo" accept-charset="utf-8">

The problem is the accept-charset is windows-1251 instead of utf-8 问题是accept-charset是windows-1251而不是utf-8

After

oReq.open("POST", "/echo/html", true);

you can also add 你也可以添加

oReq.overrideMimeType('text/html; charset=UTF-8');
oReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

but this is not what fixes the problem. 但这不是解决问题的方法。

Good luck. 祝好运。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM