最通用的Ajax中文乱码解决方案。
2009-01-08 15:36
323 查看
网上有很多乱解决方案,比如设置web.config等,感觉都不够简单。
感谢小猴告诉我最通用的方法,就是前台js中文编码escape(),后台解码Server.UrlDecode()
另外注意,如果网页高级保存选项不是utf-8,要改过来。我没有试过其他编码,总之此编码成功。
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxLuanMa.aspx.cs" Inherits="testXc_AjaxLuanMa" %>
<!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 runat="server">
<title>Ajax乱码问题</title>
<script type="text/javascript">
//输入列号,列名,monthNum更新数据库。
//如果monthNum为"空",则代表要更新的是临时表。
//如果monthNum非空,则根据月份和列号,更新模板表。
function CallServer(colName,col,monthNum)
{
arg = escape(colName) + '|' + escape(col) + '|' + escape(monthNum);
<%= ClientScript.GetCallbackEventReference(this, "arg", "OnCallBack", null) %>;
}
//回调函数,提示一下。
function OnCallBack(result,context)
{
alert(unescape(result));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="确定" onclick='CallServer("列名","列号","空");' />
</div>
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class testXc_AjaxLuanMa : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string result;
protected void Page_Load(object sender, EventArgs e)
{
}
//引发回调事件处理
public void RaiseCallbackEvent(string eventArgument)//参数是从前台传过来的字符串。
{
string[] args = eventArgument.Split('|');
//执行业务逻辑
string arg0 = Server.UrlDecode(args[0]);
string arg1 = Server.UrlDecode(args[1]);
string arg2 = Server.UrlDecode(args[2]);
if (arg2 == "空")
result = "更新临时表:" + arg0 + "和" + arg1;
else
result = "更新模板表:" + arg0 + "和" + arg1;
}
//回传回调结果
public string GetCallbackResult()
{
return result;
}
}
end
感谢小猴告诉我最通用的方法,就是前台js中文编码escape(),后台解码Server.UrlDecode()
另外注意,如果网页高级保存选项不是utf-8,要改过来。我没有试过其他编码,总之此编码成功。
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxLuanMa.aspx.cs" Inherits="testXc_AjaxLuanMa" %>
<!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 runat="server">
<title>Ajax乱码问题</title>
<script type="text/javascript">
//输入列号,列名,monthNum更新数据库。
//如果monthNum为"空",则代表要更新的是临时表。
//如果monthNum非空,则根据月份和列号,更新模板表。
function CallServer(colName,col,monthNum)
{
arg = escape(colName) + '|' + escape(col) + '|' + escape(monthNum);
<%= ClientScript.GetCallbackEventReference(this, "arg", "OnCallBack", null) %>;
}
//回调函数,提示一下。
function OnCallBack(result,context)
{
alert(unescape(result));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="确定" onclick='CallServer("列名","列号","空");' />
</div>
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class testXc_AjaxLuanMa : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
private string result;
protected void Page_Load(object sender, EventArgs e)
{
}
//引发回调事件处理
public void RaiseCallbackEvent(string eventArgument)//参数是从前台传过来的字符串。
{
string[] args = eventArgument.Split('|');
//执行业务逻辑
string arg0 = Server.UrlDecode(args[0]);
string arg1 = Server.UrlDecode(args[1]);
string arg2 = Server.UrlDecode(args[2]);
if (arg2 == "空")
result = "更新临时表:" + arg0 + "和" + arg1;
else
result = "更新模板表:" + arg0 + "和" + arg1;
}
//回传回调结果
public string GetCallbackResult()
{
return result;
}
}
end
相关文章推荐
- 最通用的Ajax中文乱码解决方案。
- extjs及ajax中文乱码问题及解决方案(基本通用)
- 关于Ajax请求中传输中文乱码问题的解决方案
- Javascript和Ajax中文乱码吐血版解决方案
- 再转一个 ajax中文乱码解决方案
- JQuery ajaxSubmit提交中文乱码的解决方案
- SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案
- ajax 文本框输入提示中文乱码问题解决方案
- JQuery ajaxSubmit提交中文乱码的解决方案
- jquery1.4.2 ajax 中文乱码问题解决方案
- ajax中文乱码解决方案
- Ajax中文乱码问题解决方案(servlet)
- AJAX中文乱码的几种后台的解决方案
- Ajax中文乱码解决方案
- JFreeChart中文乱码和文字模糊问题的通用解决方案
- Ajax(xmlhttp request)中文乱码的解决方案
- jQuery-Ajax提交数据中文乱码问题的解决方案
- 用ajax提交表单时中文乱码解决方案 推荐
- ajax获取数据中文乱码问题最简单的完美解决方案
- ajax中文乱码原因分析及解决方案