网上有很多乱解决方案,比如设置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;
- }
- }