jquery向.ashx文件post中文亂碼問題的解決方法
更新時間:2011年03月28日 22:29:07 作者:
jquery向.ashx文件post中文亂碼問題的解決方法,需要的朋友可以參考下。
1.我的環(huán)境:vs2005,未裝SP1補丁,不能創(chuàng)建Web應(yīng)用程序,只能創(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才導(dǎo)致.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.dhdzp.com/article/26658.htm
http://www.dhdzp.com/article/26659.htm
2.web.config中的相關(guān)配置
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
3.jquery的Post數(shù)據(jù)的寫法
復(fù)制代碼 代碼如下:
$(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才導(dǎo)致.ashx的context.Request.Params["StudentName"]顯示為亂碼。
感覺比較奇怪的現(xiàn)象:
現(xiàn)象1:在不添加contentType:"application/x-www-form-urlencoded; charset=UTF-8",的情況下,在.ashx文件中使用下面的語句卻可以正確顯示字符串:
復(fù)制代碼 代碼如下:
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.dhdzp.com/article/26658.htm
http://www.dhdzp.com/article/26659.htm
您可能感興趣的文章:
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- jquery中$.post()方法的簡單實例
- javascript jQuery $.post $.ajax用法
- jQuery get和post 方法傳值注意事項
- jQuery中ajax的post()方法用法實例
- jquery ajax post提交數(shù)據(jù)亂碼
- Jquery AJAX POST與GET之間的區(qū)別
- jquery post方式傳遞多個參數(shù)值后臺以數(shù)組的方式進行接收
- jquery中g(shù)et,post和ajax方法的使用小結(jié)
- jQuery post數(shù)據(jù)至ashx實例詳解
相關(guān)文章
jQuery插件FusionCharts繪制2D雙折線圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件FusionCharts繪制2D雙折線圖效果,結(jié)合實例形式分析了jQuery使用FusionCharts結(jié)合xml數(shù)據(jù)載入實現(xiàn)2D雙折線圖繪制的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-04-04
jquery實現(xiàn)tab選項卡切換效果(懸停、下方橫線動畫位移)
這篇文章主要介紹了jquery實現(xiàn)tab選項卡切換效果,實現(xiàn)懸停、下方橫線動畫位移,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Jquery修改頁面標(biāo)題title其它JS失效的解決方法
這篇文章主要介紹了Jquery修改頁面標(biāo)題title其它JS失效的解決方法,很簡單,很實用,需要的朋友可以參考下2014-10-10
基于jquery的has()方法以及與find()方法以及filter()方法的區(qū)別詳解
本篇文章介紹了,基于jquery的has()方法以及與find()方法以及filter()方法的區(qū)別詳解需要的朋友參考下2013-04-04

