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

Here is a sample 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 path through the standard submit form, then the payload request is as follows:

 ------WebKitFormBoundary2890GbzEKCmB08rz Content-Disposition: form-data; name="label" f&#229;r l&#248;bende 

But when I submit this AJAX path, then it looks a little different:

 ------WebKitFormBoundaryPO2mPRFKj3zsKVM5 Content-Disposition: form-data; name="label" für løbende 

As you can see, in the first case, some characters are replaced with character entities, but in the case of using FormData there is a simple string, which, of course, is good, because it is utf-8, but is there any way to make it behave like a standard submit form?

+6
source share
2 answers

The answer to your question: None . You cannot change it. According to XMLHttpRequest2 TR , FormData constructed data is explicitly encoded to UTF-8 . Without mentioning the possibility of changing it.

Regular mimeType or Content-Type = charset become invalid for multipart requests, because for the same reason it is handled differently.

To quote

If the data is FormData Let the body of the request object be the result of running the multipart / form-data encoding algorithm with data in the form of a form data set and with UTF-8 as explicit character encoding .

Let the mime type be the concatenation of "multipart / form-data;", the character SP + SPO U + 0020, "border =" and the border line multipart / form-data generated by the multipart / form-data encoding algorithm.

Hope this helps!

Update

If you are willing to give up

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

for

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

possible solution. Let me know if this is what you are looking for.

Another update

I ran a little. Updated violin with all modes

So this is the story

1 form with accept-charset="utf8" => default behavior

Content does not require additional screening / coding. Thus, the request is triggered with the text without changes as für løbende

2 form with accept-charset="windows-1251" => your case

The content requires additional escaping / encoding, since utf8 is used in the default encoding of the browser. Thus, the content is escaped and then launched, that is, the sent content is f&#229;r l&#248;bende

3 FormData constructed with form element

The content does not require additional escaping / encoding, since by default it is equal to utf8 . Thus, the query is triggered with text as für løbende .

4 FormData constructed, and then appended with escaped data

The content is still in utf8 encoding, but you do not need to call escape(content) before adding to the form data. This means that the request is triggered with text as f%E5r%20l%F8bende . Still no bone?

I was mistaken, no. Having looked closely [read => looking for several minutes ....] on

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

f%E5r%20l%F8bende

Then it all fell into place - %E5 (hexadecimal) = &#229; (decimal). So basically escape() is a Javascript way of doing things based on % encoding that doesn't support HTML.

Similar to &#; , as we know, is an HTML coding method. So I add another mode in ajax, [this is what you are looking for, I assume]

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

The content is still in utf8 encoding. It doesn't hurt to avoid it, like HTML coding, using this wonderful piece of code from fooobar.com/questions/21554 / .... And voila, the request was launched with the text f&#229;r l&#248;bende

Updated violin with all modes

Hope this helps sort it out!

UPDATE for full support for Windows-1251

This input für løbende did not work in earlier mode. 5. Update the script http://jsfiddle.net/epsSZ/6/ .

Uses a combination of solutions here fooobar.com/questions/964706 / ... and mine. Therefore, the problem is to avoid everything. So, now we only avoid characters that are not encoded in windows-1251.

It helps me hope!

+6
source

Thanks for this question, I enjoyed! :)
Replace

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

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

The problem is accept-charset windows-1251 instead of 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. :)

0
source

All Articles