jquery向.ashx文件post中文亂碼問題的解決方法
更新時間:2011年03月28日 22:29:07 作者:
jquery向.ashx文件post中文亂碼問題的解決方法,需要的朋友可以參考下。
1.我的環(huán)境:vs2005,未裝SP1補丁,不能創(chuàng)建Web應用程序,只能創(chuàng)建網站;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.dbjr.com.cn/article/26658.htm
http://www.dbjr.com.cn/article/26659.htm
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.dbjr.com.cn/article/26658.htm
http://www.dbjr.com.cn/article/26659.htm
相關文章
Jquery Ajax學習實例7 Ajax所有過程事件分析示例
JQuery在執(zhí)行Ajax的過程中會觸發(fā)很多事件。2010-03-03jQuery插件HighCharts繪制的基本折線圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制的基本折線圖效果,結合實例形式分析了jQuery基于HighCharts插件繪制圖形的具體實現步驟與相關操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03