XMLHttpRequest. Send NOT data to UTF-8. Is mission impossible?

I need to send data using XMLHttpRequest (POST method) to the server.

Terms and conditions:

  • The server always assumes that character encoding is for Windows-1251 only.
  • I can’t change anything on the server side. The server is not mine.
  • My code should work in FF, Chrome and IE8 :-( I experimented with sendAsBinary , but did not understand.

As I can see, XMLHttpRequest always changes the data to UTF-8 before sending, so the problem cannot be solved. Right?

+4
source share
1 answer

You say: "XMLHttpRequest always changes data to UTF-8 before sending."

WHAT IS WRONG!!! XMLHttpRequest must be encoded before sending, the encoding is determined by the document itself.

for html5, you can use <meta charset="YOUR-ENCODING-HERE"> . for html or xhtml, you should use <meta http-equiv="Content-Type" content="text/html; charset=YOUR-ENCODING-HERE-WITHOUT-QUOTES"> . for xml you can use <?xml version="1.0" encoding="YOUR-ENCODING-HERE"?> .

then before XMLHttpRequest.send () you need XMLHttpRequest.setRequestHeader("Content-type","SOME-OTHER-DECLARATION; charset=YOUR-ENCODING-HERE-WITHOUT-QUOTES"); .

Given all of the above, no matter what encoding your server uses by default, it will use the encoding offered by the offer (if it supports) to decode the URI.

There is another way to do this.

I do not know which programming language is used on your server side. For java, you can use, for example, str is the string received by the server, newStr = new String(str.getBytes("SERVER-ASSUMED-CHAR-ENCODING"), "YOUR-HTML-ENCODING");

In your case, it will be newStr = new String(str.getBytes("windows-1251"), "UTF-8"); . And newStr should be readable data.

+1
source

All Articles