ajax中文亂碼問(wèn)題解決方案
更新時(shí)間:2013年04月03日 17:05:02 作者:
ajax中文亂碼問(wèn)題在中文中經(jīng)常會(huì)出現(xiàn)這種問(wèn)題,其實(shí)只要稍加注意就不會(huì)出現(xiàn)ajax中文亂碼這回事情了,接下來(lái)為大家詳細(xì)介紹下如何解決這類問(wèn)題
ajax中文亂碼問(wèn)題在中文中經(jīng)常會(huì)出現(xiàn)這種問(wèn)題,其實(shí)只要稍加注意就不會(huì)出現(xiàn)ajax中文亂碼這回事情了,注意前后臺(tái)編碼一致.你用的是中文.而ajax傳輸數(shù)據(jù)的時(shí)候用的是utf-8 ,還有對(duì)ajax get方法時(shí)最好escape 或urlcode,
<%@ page contenttype="text/html;charset=gb2312%>
如果是用servlet就加
response.setcontenttype("text/html;charset=gb2312");
request.setcharacterencoding("gb2312");
還有一個(gè)更好的方法就是在加一個(gè)filter
在其中加入
response.setcontenttype("text/html;charset=gb2312");
request.setcharacterencoding("gb2312");
一切都解決了
再說(shuō)一下從客戶端上傳數(shù)據(jù),就必須在服務(wù)端進(jìn)行編碼轉(zhuǎn)換
string param = request.getparamter("param");
param = new string(param.getbytes("iso-8859-1"),"gb2312");
現(xiàn)在就都是中文的了。
注意前后臺(tái)編碼一致.你用的是中文.而ajax傳輸數(shù)據(jù)的時(shí)候用的是utf-8
<script>
var oxmlhttp = new activexobject( "microsoft.xmlhttp ");
oxmlhttp.open( "get ", "http://dotnet.aspx.cc/content.aspx ", false);
oxmlhttp.send()
var ostream = new activexobject( "adodb.stream ");
if(ostream == null)
alert( "您的機(jī)器不支持adodb.stream. ")
else
{
ostream.type=1;
ostream.mode=3;
ostream.open() ;
ostream.write(oxmlhttp.responsebody);
ostream.position= 0;
ostream.type= 2;
ostream.charset= "gb2312 ";
var result= ostream.readtext();
ostream.close();
ostream = null;
alert( result);
}
</script>
客戶端文件的編碼設(shè)置為gb2312,如下面代碼所示:
html代碼
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
在發(fā)送的url地址中的查詢字符串或者是使用post方式發(fā)送的請(qǐng)求內(nèi)容不要使用escape函數(shù)進(jìn)行編碼,切記!
在服務(wù)器端的jsp教程文件也設(shè)置為gb2312編碼格式,如下面代碼所示:
jsp 代碼
或者設(shè)置response的頭,如下面代碼所示:
response.setheader("content-type","text/html; charset=gb2312");
兩者原理是一樣的。
最著關(guān)鍵的是在獲取參數(shù)時(shí)應(yīng)該對(duì)獲取字符串進(jìn)行重新編碼,如下面代碼所示:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"gb2312");
其中,username為接收的參數(shù)。
直接使用out.print(username);就可以將中文返回給客戶端,在客戶端直接使用xmlhttp.responsetext屬性就可以直接使用返回的中文了!
附件中我測(cè)試用的一個(gè)小例子,在tomcat6.0和resin2.1.8中通過(guò)測(cè)試!
其實(shí),還有一個(gè)一勞永逸的解決方案,就是添加一個(gè)過(guò)濾器。
補(bǔ)充一下提交方法為get時(shí)時(shí)在服務(wù)器里寫的時(shí)這句代碼
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"gb2312");
為post時(shí)應(yīng)該時(shí)這樣吧
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"utf-8");
復(fù)制代碼 代碼如下:
<%@ page contenttype="text/html;charset=gb2312%>
如果是用servlet就加
復(fù)制代碼 代碼如下:
response.setcontenttype("text/html;charset=gb2312");
request.setcharacterencoding("gb2312");
還有一個(gè)更好的方法就是在加一個(gè)filter
在其中加入
復(fù)制代碼 代碼如下:
response.setcontenttype("text/html;charset=gb2312");
request.setcharacterencoding("gb2312");
一切都解決了
再說(shuō)一下從客戶端上傳數(shù)據(jù),就必須在服務(wù)端進(jìn)行編碼轉(zhuǎn)換
復(fù)制代碼 代碼如下:
string param = request.getparamter("param");
param = new string(param.getbytes("iso-8859-1"),"gb2312");
現(xiàn)在就都是中文的了。
注意前后臺(tái)編碼一致.你用的是中文.而ajax傳輸數(shù)據(jù)的時(shí)候用的是utf-8
復(fù)制代碼 代碼如下:
<script>
var oxmlhttp = new activexobject( "microsoft.xmlhttp ");
oxmlhttp.open( "get ", "http://dotnet.aspx.cc/content.aspx ", false);
oxmlhttp.send()
var ostream = new activexobject( "adodb.stream ");
if(ostream == null)
alert( "您的機(jī)器不支持adodb.stream. ")
else
{
ostream.type=1;
ostream.mode=3;
ostream.open() ;
ostream.write(oxmlhttp.responsebody);
ostream.position= 0;
ostream.type= 2;
ostream.charset= "gb2312 ";
var result= ostream.readtext();
ostream.close();
ostream = null;
alert( result);
}
</script>
客戶端文件的編碼設(shè)置為gb2312,如下面代碼所示:
html代碼
復(fù)制代碼 代碼如下:
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
在發(fā)送的url地址中的查詢字符串或者是使用post方式發(fā)送的請(qǐng)求內(nèi)容不要使用escape函數(shù)進(jìn)行編碼,切記!
在服務(wù)器端的jsp教程文件也設(shè)置為gb2312編碼格式,如下面代碼所示:
jsp 代碼
或者設(shè)置response的頭,如下面代碼所示:
復(fù)制代碼 代碼如下:
response.setheader("content-type","text/html; charset=gb2312");
兩者原理是一樣的。
最著關(guān)鍵的是在獲取參數(shù)時(shí)應(yīng)該對(duì)獲取字符串進(jìn)行重新編碼,如下面代碼所示:
復(fù)制代碼 代碼如下:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"gb2312");
其中,username為接收的參數(shù)。
直接使用out.print(username);就可以將中文返回給客戶端,在客戶端直接使用xmlhttp.responsetext屬性就可以直接使用返回的中文了!
附件中我測(cè)試用的一個(gè)小例子,在tomcat6.0和resin2.1.8中通過(guò)測(cè)試!
其實(shí),還有一個(gè)一勞永逸的解決方案,就是添加一個(gè)過(guò)濾器。
補(bǔ)充一下提交方法為get時(shí)時(shí)在服務(wù)器里寫的時(shí)這句代碼
復(fù)制代碼 代碼如下:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"gb2312");
為post時(shí)應(yīng)該時(shí)這樣吧
復(fù)制代碼 代碼如下:
string username = new string(request.getparameter("username").getbytes("iso8859_1"),"utf-8");
相關(guān)文章
關(guān)于Ajax技術(shù)中servlet末尾的輸出流
這篇文章主要介紹了關(guān)于Ajax技術(shù)中servlet末尾的輸出流的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
ajax jquery校驗(yàn)用戶是否已經(jīng)注冊(cè)演示代碼
用戶是否已經(jīng)注冊(cè)的判斷方法有很多,在本文將為大家詳細(xì)介紹下如何使用ajax jquery來(lái)實(shí)現(xiàn),感興趣的朋友可以參考下2013-10-10
asp.net中mvc使用ajax提交參數(shù)的匹配問(wèn)題解決探討
本文為大家介紹下使用javaScript解決asp.net中mvc使用ajax提交參數(shù)的匹配問(wèn)題,遇到類似情況的朋友可以參考下,希望對(duì)大家有所幫助2013-07-07
javascript ajax類AJAXRequest2007-12-31 更新
2008-01-01
Ajax 通過(guò)城市名獲取數(shù)據(jù)(全國(guó)天氣預(yù)報(bào)API)
本文給大家分享全國(guó)天氣預(yù)報(bào)API Ajax 通過(guò)城市名獲取數(shù)據(jù),通過(guò)html和js兩部分代碼實(shí)現(xiàn)天氣預(yù)報(bào)效果,輸入城市就會(huì)出現(xiàn)天氣情況,效果非常棒,感興趣的朋友可以參考下2016-11-11
$.ajax傳JSON數(shù)據(jù)到后臺(tái)的注意事項(xiàng)小結(jié)
這篇文章主要介紹了$.ajax傳JSON數(shù)據(jù)到后臺(tái)的注意事項(xiàng),需要的朋友可以參考下2014-05-05
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)
xmlhttp 亂碼 比較完整的解決方法 (UTF8,GB2312 編碼 解碼)...2007-09-09

