A word to solve the AJAX Chinese garbled problem [recommended]_ajax related

Source: Internet
Author: User
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.

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.