AJAX is a technology. One of the frequently used techniques of this technology is
sending requests using the class object XMLHttpRequest.How to send and receive AJAX requests in the necessary encoding, do we need to use single-byte encodings or not to do without UTF-8. This article will answer all these questions once and for all.
')
By the way, a reprint from
mine .
And yet, of course, there are no classes in JavaScript, but for convenience we will use such terminology.
The XMLHttpRequest documentation states that the browser must support the following types.
HTTP requests: GET, POST, HEAD, PUT, DELETE, OPTIONS.
Today javascript through the object of class XMLHttpRequest can be sent
only requests like GET and POST .
So, consider 2 of these requests:
1. GET type request:All information to the script on the server can be transmitted only through the URL and through the headers.
For example,
GET
moy-rebenok / ajax.php? F = 324Host: moy-rebenok
User-Agent: Mozilla / 5.0 (Windows; U; Windows NT 5.1; ru; rv: 1.8.1.11) Gecko / 20071127
Firefox / 2.0.0.11
Accept:
text / xml, application / xml, application / xhtml + xml, text / html; q = 0.9, text / plain; q = 0.8, image / pn
g, * / *; q = 0.5
Accept-Language: ru-ru, ru; q = 0.8, en-us; q = 0.5, en; q = 0.3
Accept-Encoding: gzip, deflate
Accept-Charset: windows-1251, utf-8; q = 0.7, *; q = 0.7
Keep-Alive: 300
Connection: keep-alive
Referer:
moy-rebenok / ajax.htmlOn the server, in ajax.php you can use the construction
$ _GET ['f'] to get the value of the variable f.
Why is there a problem with Russian letters? Because, as you know, Russian letters in the URL cannot be used, they must somehow be conveyed using the available Latin letters, numbers and symbols allowed in the URL after the '?'.
People have agreed that they will do this with the help of escape sequences.
The escape sequence of the word "hello" in windows-1251 encoding:
% EF% F0% E8% E2% E5% F2
escape sequence of the word "hello" in UTF-8 encoding:
% D0% BF% D1% 80% D0% B8% D0% B2% D0% B5% D1% 82
The escape sequence of the word "hello" in the KOI8-R encoding:
% CE% CF% D5% C1% C5% D0
(The sign '%', then the character code).
Thus, you can transfer Russian letters, for example, like this:
GET
moy-rebenok / ajax.php? F =% EF% F0% E8% E2% E5% F2Host: ...
or so:
GET
moy-rebenok / ajax.php? F =% D0% BF% D1% 80% D0% B8% D0% B2% D0% B5% D1% 82Host: ...
No one limits you to this .
By the way, for the GET request you
do not need to specify the Content-Type header.
Because no content There is only a request to a specific address.
All variables are transmitted to the server via the URL.
How to make the necessary escape sequence in the desired encoding?
You can even do it with your hands, at least somehow, but of course in JavaScript.
Again, no one limits you.
But for convenience, usually use one of the 3 functions that are already defined in JavaScript:
a) escape ()
b) encodeURI ()
c) encodeURIComponent ()
In order:
a) escape ()
Latin letters, numbers, @ * / + symbols. leaves it as it is, everything else is encoded like this:
% xx or% uxxxx.
Moreover, xxxx in the second case is the character code not in UTF-8, but in Unicode.
(
Difference between Unicode and UTF-8 ).
It is
not necessary to use this function, since the output depends on the browser, the function is not standardized by the W3C, originated in the dashing 90s.
In addition, somehow normal (at least quickly) to process a string in such a vinyl format on the server is difficult.
The escape () function is used by our compatriot's JsHttpRequest library.
Not because the library is bad, but because it was created to work with all browsers.
(including the oldest).
b) encodeURI ()
Latin letters, numbers, symbols! @ # $ & * () =: /;? + '. leave as is, everything else
encodes
UTF-8 encoded escape sequences.
c) encodeURIComponent ():
Latin letters, numbers, symbols! * () '. leaves as is, everything else encodes
UTF-8 encoded escape sequences.
Approved by W3C.
Used jQuery, prototype.js when requested by the method GET.
Perhaps you heard from someone: "XMLHttpRequest only works with UTF-8."
Now you know that this is not entirely true.
When a GET request is used, the encoding of the transferred data is not written anywhere at all (!).
I repeat, the 'Content-type', in which we can specify the charset
not used in GET requests.
But, since In JavaScript there are 2 convenient functions for translating any string into a string with escape sequences in UTF-8, then they all use them and work with UTF-8.
That is why in jQuery you can’t even specify the charset when sending a request.
That is why in Prototype.js, even when you specify encoding = 'windows-1251', and you use a GET request, UTF-8 is transmitted anyway.
Just because the codes of these libraries use the encodeURIComponent () function.
Well. There is absolutely nothing wrong with that. All you need to do to work now
in php in
normal encoding use iconv:
$ f = iconv ('UTF-8', 'windows-1251', $ _GET ['f']);
By the way, we can do it precisely because $ _GET works in such a way that it understands
escape sequences Thanks to the creators of PHP.
Those. when a GET request arrives, PHP looks at the URL, creates an $ _GET array for us, and we
already with him
what we want, we do. But it seems to be understandable.
2) POST requests.Here everything is more interesting.
Here comes this request to the server. The PHP handler looks at the Content-type, and depending on it fills the $ _POST array and / or the $ HTTP_RAW_POST_DATA variable.
$ _POST it fills in the case when the Content-type is multipart / form-data or
x-www-form-urlencoded.
What is this Content-type?
A content type is very convenient. It allows you to pass several variables to the php script.
What is essentially a POST request?
These are headlines and content after them. Content is generally arbitrary. Those. just bytes, bytes, bytes.
But after all, from JavaScript it is usually required to transfer not just bytes, bytes, bytes, but several pairs of key = value, key = value, ...
As in the GET request.
So people agreed on such a convenient type as x-www-form-urlencoded
In order to pass f = 123 and gt = null, you must pass the content:
f = 123 & gt = null
Isn't it familiar? Certainly familiar, and the type is not for nothing called x-www-form-urlencoded.
All the same as with the GET request.
And how is the content generated in the jQuery and prototype.js libraries?
True, with the help of the same function encodeURIComponent (), and therefore the escape sequences will be in UTF-8 encoding. (Regardless of what you set in encoding.js).
Everything. There is one more opportunity. After all, you can not transfer x-www-form-urlencoded (ie, not parameters), but plain text or binary content, which can then be read through $ HTTP_RAW_POST_DATA.
To do this, set the Content-type text / xml or application / octet-stream, and set charset = "windows-1251" there.
We insert the string of the required encoding into the send () function. (Prototype.js wraps this call with the construct new Ajax.Request (...)).
And then ... And he (the object of the class XMLHttpRequest) translates this string into UTF-8, whatever the encoding it is. This is written in the W3C documentation. And he really does it.
Findings:1. Directly through XMLHttpRequest, you can send only UTF-8 strings.
2. It is possible to transmit strings as if “in any other encodings”, if non-Latin characters
at the same time, over-escape.
3. In JavaScript, there are 3 functions that escape non-Latin characters:
escape (), encodeURI () and encodeURIComponent ().
The first translates into a Unicode curve. The second two are in UTF-8.
You can write your own functions that will generate the escape sequences of any encoding. It is possible, but not necessary. Because on the contrary, we should be glad that there are such functions that translate the text of
any encoding into UTF-8. This is an extremely beautiful fact. The scheme in which all xhtml pages work on windows-1251, ajax from the server to the client throws windows-1251, and ajax from the client to the server throws UTF-8 is
absolutely acceptable and is used on most resources .
Just do not forget to use
iconv as described below. And in order for the server to give javascript JSON (or whatever you have) in the correct encoding (i.e., in the same encoding in which all xhtml pages are sent) just write the heading at the beginning of your ajax.php:
header ('Content-type: text / html; charset = windows-1251');
And everything will be ok.
Lastly, some subjective opinion:
Use jQuery, love people, give gifts.