Many of them may be distorted or unverified during the transfer process, but they cannot be used in real time. Therefore, they decided to perform a comprehensive test on their own, and the effort was not in vain, although the final result is very simple, the process is really tiring for a new Ajax student.
You are welcome to share your new experiences with us. One thought + one thought can produce at least two thoughts.
1. When get is sent:
Method 1: Use response. charset = "gb2312" on the ASP server to define the output code to the client.
In this case, the client does not need to perform any conversions. The following two files:
1. Client JS CopyCode The Code is as follows: var XMLHTTP;
Function createxml (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
} Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}
Function startxml (){
Createxml ();
XMLHTTP. onreadystatechange = handlestatechange;
VaR url = "ajaxtext. asp? TM = 1 & CC "+ math. Random ();
// Var sendcontents = 'theinput = '+ escape (theinput. value );
VaR regcode = "2abc ";
XMLHTTP. Open ("get", URL, true );
// Regcode = "regcode =" + regcode;
// XMLHTTP. setRequestHeader ("Content-Length", regcode. Length); // you can add or remove
// XMLHTTP. setRequestHeader ("cache-control", "No-Cache ");
// XMLHTTP. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded'); // do not add
// XMLHTTP. Send (null );
// XMLHTTP. Send ("regcode =" + escape (regcode ));
XMLHTTP. Send (null );
}
Function handlestatechange (){
If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 ){
// Var divid = Document. getelementbyid ("Results ");
Retext = XMLHTTP. responsetext;
Alert (retext );
// If (divid. haschildnodes ()){
// Divid. removechild (divid. childnodes [0]);
//}
// Var result = Document. createtextnode (XMLHTTP. responsetext );
// Divid. appendchild (result );
// Document. getelementbyid ("Results"). innerhtml = XMLHTTP. responsetext;
}
}
server: copy Code the code is as follows: <% response. codePage = 936%>
<% response. charset = "gb2312"
dim Reg
Reg = request ("regcode")
response. write "Mr. Wang and his friends" can be output correctly.
%>
Method 2: Use Function Conversion on the client (from the Internet ). copy Code the code is as follows: function gb2utf8 (data) {
var glbencode = [];
gb2utf8_data = data;
execScript ("gb2utf8_data = midb (gb2utf8_data, 1 )", "VBScript");
var T = escape (gb2utf8_data ). replace (/% u/g ,""). replace (/(. {2 })(. {2})/g, "%$ 2% $1 "). replace (/% ([A-Z].) % (. {2})/g, "@ $1 $2");
T = T. split ("@");
var I = 0, j = T. length, K;
whil E (++ I K = T [I]. substring (0, 4);
If (! Glbencode [k]) {
gb2utf8_char = eval ("0x" + k);
execScript ("gb2utf8_char = CHR (gb2utf8_char)", "VBScript ");
glbencode [k] = escape (gb2utf8_char ). substring (1, 6);
}< br> T [I] = glbencode [k] + T [I]. substring (4);
}< br> gb2utf8_data = gb2utf8_char = NULL;
return Unescape (T. join ("%");
}
At this point if the server side does not specify the encoding, then Ajax is the UTF-8 by default, can not be displayed on the client, you can use this function.Copy codeThe Code is as follows: retext = XMLHTTP. responsetext;
Retext = gb2utf8 (retext );
Alert (retext );
3. In Firefox: For details, refer to: NextArticle
Firefox is relatively simple. It supports XMLHTTP. responsetext; // Firefox. But to simplify the code, we recommend that you use the preceding Code directly.
However, for general purpose, follow the methods described below in post.
Ii. When post is sent:
For general Chinese, the above get method can also be used in post, but some symbols cannot be displayed in an article on the Internet, such: in the next article, "test · test + test ·" (in the middle of the interval number) has been tested. It it is true that after the server end defines the encoding, it cannot be output to the client.
If,
The sender uses XMLHTTP. Send ("regcode =" + escape (regcode ));
Then, the server puts the received data on two encoding encodeuricomponent (escape (xxxxxxx). The result is: Test % 25u7a5eest % 2520 test. In addition, this idea should be wrong. I don't know if this article is wrong when it is transferred online. It should be reversed on the server end after the client uses escape encoding for transmission. Or the request itself has the function of decoding. No second or third encoding is required. Besides, escape and encodeuricomponent cannot encode special characters.
I made the following output on the server:Copy codeThe Code is as follows: Test2 = "test · test + test · feedback :"
Response. Write Test2 & "is a special symbol" · "that is directly output by the server rather than received by the server. The following is the received data (the content is the same as that sent by the original Ajax for comparison ):"
That is to say, a special character is generated directly on the server. The server file has been encoded as gb2312 using <% response. charset = "gb2312" %>. However, the output result is that the Test2 = "test · test + test · feedback generated on the server side:" The output can be normal, but the received Ajax value will not work.
Even if you try to use the three JS encoding parameters escape () \ encodeuri () \ encodeuricomponent () on the client, and then output the code at the server provider. This indicates that the special characters in the string have been deformed when the server receives them.
Now the problem is defined in encoding transfer. To illustrate this problem, I made another small experiment: assign this string to a variable in ASP and then assign it to JS using the ASP variable, as follows:Copy codeThe Code is as follows: <%
Para = "test · test + test ·/"
Para = server. urlencode (para)
%>
Then hand over the JS variable, as shown below:Copy codeThe Code is as follows: XMLHTTP. Open ("Post", URL, true );
VaR regcode = "<% = para %> ";
XMLHTTP. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded; charset = gb2312 ');
XMLHTTP. Send ("regcode =" + regcode );
The result is normal, which fully demonstrates that ASP server. urlencode can fully encode special characters. JS is still lacking.
What should we do now?
If there is no way, I think that encodeuricomponent () can be more special character encoding than encode (). Then, like multiple MD5 encryption, it may be better to increase the hardening agent, so I applied two encodeuricomponent (), and the current code is:Copy codeThe Code is as follows: var regcode = "test · test + test ·/";
XMLHTTP. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded; charset = gb2312 ');
XMLHTTP. Send ("regcode =" + encodeuricomponent (regcode )));
The above is the client, and then change the server:Copy codeThe Code is as follows: <script language = "JavaScript" runat = "server">
Function decodestr (STR ){
Return decodeuricomponent (STR ));
}
</SCRIPT>
<%
Dim Reg
Reg = request ("regcode ")
Test2 = "test · test + test · feedback :"
Response. Write Test2 & "is a special symbol" · "that is directly input by the service rather than received by the Service. The following is the received data (the content is the same as that sent by the original Ajax for comparison ):"
Response. Write decodestr (REG)
%>
Finally, we can see the special characters output by Ajax. Both the interval number and the "+" number are displayed,
"Test · test + test ·/"
For further verification, I added the following special characters that encodeuricomponent cannot encode, as shown below:Copy codeThe Code is as follows: var regcode = "starting with a Chinese character :! @ # $ & * (·) = :/;? + Ends with special characters in the middle. ";
The results are also normal, great!
However, the data transmitted by ajax to the server is usually applied, otherwise it does not make sense. To further verify whether the data is the same, I made a small modification on the server side, as shown below:Copy codeThe Code is as follows: <%
Dim Reg
Reg = request ("regcode ")
Test2 = "starting with a Chinese character :! @ # $ & * (·) = :/;? + Ends with special characters in the middle. "
If Test2 <> decodestr (REG) then
Response. Write "different"
Else
Response. Write "same"
End if
%>
Very excited. The client output is "the same", so you can safely apply it in all aspects.
Summary:
1. the Ajax client uses encodeuricomponent () to encode the post data.
2. Use decodeuricomponent () to decode the server:
<Script language = "JavaScript" runat = "server">
Function decodestr (STR ){
Return decodeuricomponent (STR ));
}
</SCRIPT>
3. When receiving responsetext, no anti-encoding is required. Direct: retext = XMLHTTP. responsetext;
4. This line on the server is still indispensable: <% response. charset = "gb2312" %>, usually in ASP files, there are <% @ Language = "VBScript" codePage = "936" %> lines, but this line can be removed, <% response. charset = "gb2312" %>. Otherwise, an error occurs.
Now, you can memorize encodeuricomponent ()/decodeuricomponent.
Appendix: The following encoding methods are taken from the JS manual for your reference:
Escape () method:
The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % XX format (XX equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. The Unescape method is the opposite. Characters not encoded by this method: @ */+
Note: You can use Unescape () to decode the escape () encoded string. However, ecmascript V3 opposed this method. The application uses decodeuri () and decodeuricomponent () to replace it.
Encodeuri () method: ------ note that the next one is the size I -- "I" is not l, and the following decodeuri is not l.
Convert a URI string to an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'
Encodeuricomponent () method:
Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeuri (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()
Reference content
Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is gb2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeuri or encodeuricomponent.
In addition, encodeuri/encodeuricomponent is introduced after javascript1.5, and escape is available in javascript1.0.
Decodeuri () function
The decodeuri () function can decode the URI encoded by the encodeuri () function.
<SCRIPT type = "text/JavaScript">
VaR test1 = http://www.w3school.com.cn/My first/
Document. Write (encodeuri (test1) + "<br/> ")
Document. Write (decodeuri (test1 ))
</SCRIPT>
Decodeuricomponent () function: decodes the URI encoded by the encodeuricomponent () function.
Unescape (): Decode escape.
Author: non-physical life blog traindiy