一句话解决AJAX中文乱码问题[推荐]

下面是我的程序
HTML : 
复制代码 代码如下:





无标题页




请输入姓名:








request.ashx :
Code 
复制代码 代码如下:

<%@ 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 ="来自服务器的信息:您好 "+context.Request.QueryString["val"].ToString()+" --by time:"+DateTime.Now.ToLongTimeString();
context.Response.Write(tab);
}
public bool IsReusable {
get {
return false;
}
}
}

baidu搜了一大堆 大致意思是 AJAX提交数据时,使用的是UTF-8的编码 并且不可以设置为其他格式
如何解决呢 最后发现一个JS的函数escape与unescape 用escape()对将要提交的汉字进行编码,会出现大致%10%20的字符,类似与.NET中Server.UrlEncode()与Server.UrlDecode();
将JS获得的表单值进行重新编码
Code
复制代码 代码如下:

var txtval=escape(document.getElementById("txt").value);
OK, 问题解决!
其他可能还有别的办法至今没遇到 希望这个办法能帮到遇到这种困境的朋友

你可能感兴趣的:(一句话解决AJAX中文乱码问题[推荐])