Jquery强大的功能越来越收到广大朋友的喜爱,不过Ajax方面就有点太不照顾中国人民的感情了

数据传递编码仅支持UTF-8,这个虽然是全球统一编码,但是也要考虑一下中国人名本地网站的编码问题吧

而且在jquery的发送端,无论你设置了程序级编码还是页面级编码,jquery都会使用utf-8的编码方式传递过去

有些网站都运营好长事件了,完全修改UTF-8确实有点困难,好了牢骚就发到这里,下面是自己写的一个解决例子

希望能对大家有一些帮助。

原理:

在ajax发送端使用escape方法。然后在接收端(显示)使用 unescape方法转换

jquery的ajax传递参数都是转换成utf-8编码的,所以有发送前加上url编码函数escape(),之后asp.net接收处理为Server.UrlDecode(Request[参数])

例子如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="DYJ.stockSms.test" %>
<!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>无标题页</title>
    
<script type="text/javascript" src="jq.js"></script>
    
<script>
        
function hq() {
            $.post(
"test.aspx", { "stock": escape("www结合中文测试123") }, function(data) {
            $(
"#show").html(unescape(data));  //这里unescape一下就可以了 
                }
            );   
        }
    
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<input type=button value="测试" onclick="hq();" runat="server" />
    
</div>
    
<span id="show"></span>
    
</form>
</body>
</html>

后台代码:  

  protected void Page_Load(object sender, EventArgs e)
        {

            
if (Request["stock"!= null)
            {
                Response.Expires 
= -1;
                Response.Write(Server.UrlDecode(Request[
"stock"]));
                Response.End();
            }
        }

 

posted on 2009-08-02 22:38  钱途无梁  阅读(1955)  评论(1编辑  收藏  举报