欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery向.ashx文件post中文亂碼問題的解決方法

 更新時間:2011年03月28日 22:29:07   作者:  
jquery向.ashx文件post中文亂碼問題的解決方法,需要的朋友可以參考下。
1.我的環(huán)境:vs2005,未裝SP1補丁,不能創(chuàng)建Web應用程序,只能創(chuàng)建網(wǎng)站;jquery版本1.5.1

2.web.config中的相關(guān)配置

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

3.jquery的Post數(shù)據(jù)的寫法
復制代碼 代碼如下:

$(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文件中接收參數(shù)的寫法

string strStudentName = context.Request.Params["StudentName"];
注意:如果沒有contentType:"application/x-www-form-urlencoded; charset=UTF-8",則context.Request.Params["StudentName"]是亂碼。
經(jīng)過在.ashx中跟蹤context.Request.ContentEncoding,可知jquery所post過來的數(shù)據(jù)采用的是gb2312編碼,可能context.Request在接收到數(shù)據(jù)時默認采用utf-8進行解碼,但是jquery在Post數(shù)據(jù)的時候卻不是用的utf-8才導致.ashx的context.Request.Params["StudentName"]顯示為亂碼。
感覺比較奇怪的現(xiàn)象:
現(xiàn)象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"));

現(xiàn)象2:將web.config中的相關(guān)配置改為
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
之后,不管是否加上contentType:"application/x-www-form-urlencoded; charset=UTF-8",后臺的.ashx文件接收到的參數(shù)仍然是亂碼。修改web.config之后網(wǎng)站編譯的很慢且運行的也很慢。

參考文章:
http://www.dbjr.com.cn/article/26658.htm
http://www.dbjr.com.cn/article/26659.htm

相關(guān)文章

最新評論