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.