最通用的Ajax中文乱码解决方案。

网上有很多乱解决方案,比如设置web.config等,感觉都不够简单。

感谢小猴告诉我最通用的方法,就是前台js中文编码escape(),后台解码Server.UrlDecode()

另外注意,如果网页高级保存选项不是utf-8,要改过来。我没有试过其他编码,总之此编码成功。

前台:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxLuanMa.aspx.cs" Inherits="testXc_AjaxLuanMa" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>Ajax乱码问题</title>
  6.     <script type="text/javascript">
  7.         //输入列号,列名,monthNum更新数据库。
  8.         //如果monthNum为"空",则代表要更新的是临时表。
  9.         //如果monthNum非空,则根据月份和列号,更新模板表。
  10.         function CallServer(colName,col,monthNum) 
  11.         { 
  12.            arg =  escape(colName) + '|' + escape(col) + '|' + escape(monthNum);
  13.            <%= ClientScript.GetCallbackEventReference(this, "arg", "OnCallBack", null) %>;  
  14.         } 
  15.         //回调函数,提示一下。
  16.         function OnCallBack(result,context) 
  17.         {    
  18.             alert(unescape(result)); 
  19.         } 
  20.     </script>
  21. </head>
  22. <body>
  23.     <form id="form1" runat="server">
  24.     <div>
  25.         <input type="button" value="确定" onclick='CallServer("列名","列号","空");' />
  26.     </div>
  27.     </form>
  28. </body>
  29. </html>

后台:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class testXc_AjaxLuanMa : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
  12. {
  13.     private string result;
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     //引发回调事件处理
  18.     public void RaiseCallbackEvent(string eventArgument)//参数是从前台传过来的字符串。
  19.     {
  20.         string[] args = eventArgument.Split('|');
  21.         //执行业务逻辑
  22.         string arg0 = Server.UrlDecode(args[0]);
  23.         string arg1 = Server.UrlDecode(args[1]);
  24.         string arg2 = Server.UrlDecode(args[2]);
  25.         if (arg2 == "空")
  26.             result = "更新临时表:" + arg0 + "和" + arg1;
  27.         else
  28.             result = "更新模板表:" + arg0 + "和" + arg1;
  29.     }
  30.     //回传回调结果 
  31.     public string GetCallbackResult()
  32.     {
  33.         return result;
  34.     }
  35. }

end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值