Solution to AJAX garbled characters in ASP. NET

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.