Below is my program
HTML:
Copy codeThe 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:
Code
Copy 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.
Code
Copy 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.