• 最通用的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. }
  • 相关阅读:
    统计脚本代码行数
    expr算术运算
    lsof命令
    测试当前机器可以创建多少线程
    守护进程写日志
    文件描述符fd,struct files_struct
    linux查看反汇编
    信号补充
    Windows10获取VS管理员权限总是很烦人
    asp.net中的Filter类型其实是被当作单例的
  • 原文地址:https://www.cnblogs.com/JensonBin/p/2236707.html
Copyright © 2020-2023  润新知