javascript不能传递含有中文汉字的url问题

利用JavaScript通过URL方式向后台代码传值是一种经常用到的手段,但在传递汉字时经常会出现字符不全或变成乱码的问题,其原因是由于客户端IE浏览器的编码方式为GB2312(简体中文版WINDOWS的默认设置),而后台的C#代码使用utf8编码(创建WEB工程的默认配置)。 
网上有很多方案解决该问题,如将web.config的编码方式改为GB2312、在客户端通过escape先编码再传,个心体会都不是很理想或有些特殊字符不支持。经过比较我决定使用encodeURIComponent在客户端进行编码,再传值,除了“/”不支持(但实际开发中很少需要传递该值,如果真有此请况,再加一层判断即可。 
encodeURIComponent的帮助文档如下: 
encodeURIComponent 方法 
将文本字符串编码为一个统一资源标识符 (URI) 的一个有效组件。 
encodeURIComponent( encodedURIString )  
必选的 encodedURIString 参数代表一个已编码的 URI 组件。 
说明 
encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent ,那么将返回初始的字符串。因为encodeURIComponent 方法对所有的字符编码,请注意,如果该字符串代表一个路径,例如 /folder1/folder2/default.html ,其中的斜杠也将被编码。这样一来,当该编码结果被作为请求发送到 web 服务器时将是无效的。如果字符串中包含不止一个 URI 组件,请使用 encodeURI 方法进行编码。 
要求 
版本 5.5 
请参阅 
decodeURI 方法 | decodeURIComponent 方法 
应用于: Global 对象 


做了一个小例子来展现该效果   
Default.aspx代码: 
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
<!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>  
</head>  
<script type="text/javascript" language="javascript">  
    function callURL(Value1, Value2)  
    {  
        document.URL = "Default.aspx?Value1=" + encodeURIComponent(Value1) + "&Value2=" + encodeURIComponent(Value2);  
    }  
</script>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        Value1=<input id="Text1" type="text" value="1234567890"/><br />  
        Value2=<input id="Text2" type="text" value="中华人民共和国"/>  
        <br />  
        <input id="Button1" type="button" value="提交" οnclick="callURL(Text1.value, Text2.value)"/></div>  
    </form>  
</body>  
</html>  




Default.aspx.cs代码: 
复制代码 代码如下:
using System;  
using System.Data;  
using System.Configuration;  
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 _Default : System.Web.UI.Page   
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        string tmpValue1 = "";  
        string tmpValue2 = "";  
        if (Request.QueryString["Value1"] != null)  
        {  
            tmpValue1 = Request.QueryString["Value1"].ToString();  
        }  
        if (Request.QueryString["Value2"] != null)  
        {  
            tmpValue2 = Request.QueryString["Value2"].ToString();  
        }  
        Response.Write("Value1=" + tmpValue1 + "<br />" + "Value2="  + tmpValue2);  
    }  
}


详细出处参考:http://www.jb51.net/article/9305.htm

详细出处参考:http://www.jb51.net/article/9305.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值