我用ajax的方式传值到后台,如果传的值是中文时,后台获取的值会变乱码,
我的前端代码如下:
$.get("../Ajax/GetSpecialBank.ashx?BankName=" + vendor.BankName, function (result) { isSpecialBank = result; });后台获取值的代码如下:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; bool isSpecialBank = false; if (!string.IsNullOrEmpty(context.Request.QueryString["BankName"])) { SpecialBank bankInfo = PayeeInfoBLL.GetSpecialBank(context.Request.QueryString["BankName"].Trim()); if (bankInfo != null) { isSpecialBank = true; } } context.Response.Write(isSpecialBank); } public bool IsReusable { get { return false; } }
现在用context.Request.QueryString["BankName"]获取的中文值会出现乱码的现象,这个问题怎么解决?
你可以用encodeURI对传递的参数进行两次编码,在后台再通过System.Web.HttpUtility.UrlDecode进行解码就可以了。对于你这个例子具体如下操作:
前台代码:
后台获取参数:
string bankName=System.Web.HttpUtility.UrlDecode(context.Request.QueryString["BankName"]);