手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jquery向.ashx文件post中文乱码问题的解决方法
jquery向.ashx文件post中文乱码问题的解决方法
摘要:1.我的环境:vs2005,未装SP1补丁,不能创建Web应用程序,只能创建网站;jquery版本1.5.12.web.config中的相关...

1.我的环境:vs2005,未装SP1补丁,不能创建Web应用程序,只能创建网站;jquery版本1.5.1

2.web.config中的相关配置

<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>

3.jquery的Post数据的写法

复制代码 代码如下:

$(document).ready(function (){

$("#btnSend").click(function(){

$.ajax({

type: "POST",

url: "PrecisionAHandle.ashx",

contentType:"application/x-www-form-urlencoded; charset=UTF-8",

data: { "StudentId": $("#LblStudentId").attr("innerText"),"StudentName": $("#LblStudentName").attr("innerText"),"StudentAge": $("#txtStudentAge").attr("value")},

success: function(html){

$("#TabContainer").html(html);

}

});

});

});

其中StudentName是中文

4.在.ashx文件中接收参数的写法

string strStudentName = context.Request.Params["StudentName"];

注意:如果没有contentType:"application/x-www-form-urlencoded; charset=UTF-8",则context.Request.Params["StudentName"]是乱码。

经过在.ashx中跟踪context.Request.ContentEncoding,可知jquery所post过来的数据采用的是gb2312编码,可能context.Request在接收到数据时默认采用utf-8进行解码,但是jquery在Post数据的时候却不是用的utf-8才导致.ashx的context.Request.Params["StudentName"]显示为乱码。

感觉比较奇怪的现象:

现象1:在不添加contentType:"application/x-www-form-urlencoded; charset=UTF-8",的情况下,在.ashx文件中使用下面的语句却可以正确显示字符串:

复制代码 代码如下:

StreamReader steamRd = new StreamReader(HttpContext.Current.Request.InputStream);

string strPostData = steamRd .ReadToEnd();

strPostData =HttpUtility.UrlDecode(strPostData, Encoding.GetEncoding("utf-8"));

现象2:将web.config中的相关配置改为

<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>

之后,不管是否加上contentType:"application/x-www-form-urlencoded; charset=UTF-8",后台的.ashx文件接收到的参数仍然是乱码。修改web.config之后网站编译的很慢且运行的也很慢。

参考文章:

http://www.jb51.net/article/26658.htm

http://www.jb51.net/article/26659.htm

【jquery向.ashx文件post中文乱码问题的解决方法】相关文章:

js跨域请求的5中解决方式

jquery读取xml文件实现省市县三级联动的方法

js实现鼠标经过表格行变色的方法

js去除字符串里中文与空格的例子

js光标定位文本框回车表单提交问题的解决方法

jQuery实现表格行上移下移和置顶的方法

jQuery切换所有复选框选中状态的方法

JQuery插件jcarousellite的参数中文说明

JQuery中节点遍历方法实例

jQuery获得字体颜色16位码的方法

精品推荐
分类导航