Ajax garbled text is a problem that programmers often encounter. What we want to talk about today isAsp tutorial. net ajax garbled solutionOh, The following describes the processing method in detail, and then gives an example.
Use asp.net tutorial
String s = system. text. encoding. utf8.getstring (request. binaryread (request. contentlength ));
System. collections. specialized. namevaluecollection form = httputility. parsequerystring (s );
Text_supplier.text = form [text_supplier.uniqueid];
// Order. remark = text_supplier.text;
Use escape
<%
Liststr = "a simple solution to ajax Chinese garbled characters"
Parse SE. write escape (liststr) 'encoded with escape
%>
2. Client webpage special effect program
Function toserver (url)
{
Var req = new xmlhttprequest ();
If (req ){
Req. onreadystatechange = function (){
If (req. readystate = 4)
{
If (req. status = 200 | req. status = 304 ){
Getstr = unescape (req. responsetext) 'is decoded using unescape
Alert (getstr );
}
Else
{Return false ;}
}
}
Req. open ('GET', url );
Req. setrequestheader ('content-type', 'application/x-www-form-urlencoded; charset = utf-8 ');
Req. send (null );
}
}
To sum up the above example, let's look at a detailed asp.net ajax garbled processing method.
Var url = service. aspx? Value = '+ escape (va );
Obj = createajax ();
If (obj)
{
Obj. open ('GET', url, true );
Obj. send (null );
}
Asp.net's escape function
Private string escape (string s)
{
Stringbuilder sb = new stringbuilder ();
Byte [] ba = system. text. encoding. unicode. getbytes (s );
For (int I = 0; I <ba. length; I + = 2)
{/** // Be sure 2's
Sb. append ("% u ");
Sb. append (ba [I + 1]. tostring ("x2 "));
Sb. append (ba [I]. tostring ("x2 "));
}
Return sb. tostring ();
}
Asp.net processing page
Response. contenttype = "text/html ";
Response. charset = "gb2312 ";
Response. clear ();
Response. write (escape (returned string ));
Note:
By default, ajax sends encoding in the uft-8 mode.