One-sentence solution to Ajax Chinese garbled problem [recommended]

Source: Internet
Author: User

Below is my Program
HTML: CopyCode The Code is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR XMLHTTP;
Function createxmlhttprequest ()
{
If (window. activexobject)
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest ();
}
}
Function indata ()
{
VaR txtval = Document. getelementbyid ("TXT"). value;
Createxmlhttprequest ();
XMLHTTP. Open ("get", "request. ashx? Val = "+ txtval, true );
XMLHTTP. onreadystatechange = getdata;
XMLHTTP. Send (null );
}
Function getdata ()
{
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
Document. getelementbyid ("showdt"). innerhtml = XMLHTTP. responsetext;
}
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" Action = "">
<Div> enter the name:
<Input type = "text" id = "TXT"/>
<Input type = "button" value = "Submit" id = "ASDF" onclick = "indata ()"/>
<Span id = "showdt"> </span>
</Div>
</Form>
</Body>
</Html>

Request. ashx:
CodeCopy codeThe Code is as follows: <% @ webhandler Language = "C #" class = "request" %>
Using system;
Using system. Web;
Public class request: ihttphandler {
Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
String tab = "information from the server: Hello" + context. request. querystring ["Val"]. tostring () + "-- by time:" + datetime. now. tolongtimestring ();
Context. response. Write (Tab );
}
Public bool isreusable {
Get {
Return false;
}
}
}

Baidu searched for a lot, which roughly means that when Ajax submits data, it uses the UTF-8 encoding and cannot be set to another format.
How can we solve this problem? Finally, we found that a JS function escape and Unescape used escape () to encode the Chinese characters to be submitted, and there would be roughly % 10% 20 characters, similar.. NET Server. urlencode () and server. urldecode ();
Recode the form value obtained by Js.
CodeCopy codeThe Code is as follows: var txtval = escape (document. getelementbyid ("TXT"). value );

OK. Solve the problem!
there may be other ways to solve this problem. I hope this method can help my friends who have encountered this problem.

Related Article

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.