Here's my program.
Html:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled 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>
<body>
<form id= "Form1" action= "" >
<div> Please enter your name:
<input type= "text" id= "txt"/>
<input type= "button" value= "Submit" id= "asdf" onclick= "Indata ()"/>
<span id= "SHOWDT" ></span>
</div>
</form>
</body>
REQUEST.ASHX:
Code
Copy Code code 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 a whole bunch of things roughly mean that when Ajax submits data, it uses UTF-8 encoding and cannot be set to another format
How to solve it? Finally found a JS function escape and unescape with Escape () to be submitted to the Chinese character encoding, will appear roughly%10%20 characters, similar with. NET in Server.URLEncode () and Server.urldecode ();
Re-encode the form values obtained by JS
Code
Copy Code code as follows:
var txtval=escape (document.getElementById ("TXT"). value);
OK, problem solved!
There are other options that have not been met with the hope that this will help friends who have met this dilemma.