ajax post中文亂碼解記方法與執行個體說明

來源:互聯網
上載者:User

當調用request.getparameter()函數時,會自動進行一次uri的解碼過程,調用時內建的解碼過程會導致亂碼出現。而uri 編碼兩次後,request.getparameter()函數得到的是原資訊uri編碼一次的內容。再用可控的解碼函數 java.net.urldecoder.decode()就可解出原始的正確的資訊

response.setheader("charset","gb2312");
看到的說明原文如下:

用ajax中採用get來讀取頁面時,responsetext裡面的中文多半會出現亂碼,這是因為xmlhttp在處理返回的responsetext的時候,resposebody預設採用utf-8編碼,如果伺服器送出的確實是utf-8的資料流的時候漢字會正確顯示,而讀取的頁面編碼是gbk或者gb2312時就會出現頁面中文的亂碼。
解決的辦法有二:

1、在送出的流裡面加一個header,指明送出的是具體編碼流:

php教程:header('content-type:text/html;charset=gb2312');
asp教程:response.charset("gb2312")
jsp教程:response.setheader("charset","gb2312");

2、採用utf-8編碼來儲存html文檔


看個ajax jsp亂碼解決方案

ajaxpost.js
var myrequest; // variable to hold request object
function mysubmit(){
if (window.xmlhttprequest)
{
    myrequest = new xmlhttprequest(); // standards-compliant browsers
} else if (window.activexobject)
{
    myrequest = new activexobject("msxml2.xmlhttp"); // for ie
}

var post="name="+document.getelementbyid("postval").value;
post=encodeuri(post);
post=encodeuri(post); //最重要的部分,兩次調用encodeuri ,就是編碼兩次
myrequest.open("post","servlet/display",false);
//myrequest.setrequestheader("contentlength",post.length);
myrequest.setrequestheader("content-type","application/x-www-form-urlencoded");
myrequest.send(post);
var res=myrequest.responsetext;//接收返回的資料
document.getelementbyid("display").innerhtml=res;
}

ajaxpost.jsp

<%@ page language="java" import="java.util.*" pageencoding="gb2312"%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
  <head>
    <title>my jsp 'ajaxpost.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">

<script type="text/網頁特效" src="js/ajaxpost.js"></script>

  </head>
  
  <body>
     <textarea rows="10" cols="20" id=postval></textarea>
     <br>
     <input type=button onclick="mysubmit();" value="列印">
     <div id="display"></div>
  </body>
</html>

servlet

public void dopost(https教程ervletrequest request, httpservletresponse response)
   throws servletexception, ioexception {

  response.setcontenttype("text/html");
  response.setcharacterencoding("gbk");
  printwriter out = response.getwriter();
  string name=request.getparameter("name");
  name=urldecoder.decode(name,"utf8"); //post 傳遞的時候,一定是用utf8編碼的,url 自己可以設定
  system.out.println(name);
  out.println(name);
  out.flush();
  out.close();
}

執行個體二

初始頁面內容如下(hello.jsp):

<%@ page language="java" import="java.util.*" pageencoding="gb18030"%>
<%string path = request.getcontextpath();%>
<!doctype html public "-//www.111cn.net//dtd html 4.01 transitional//en">
<html>
  <head>
    <title>ajax提交頁面</title>
 <meta http-equiv="content-type" content="text/html; charset=gb18030">
   <script type="text/javascript">
    function justdo(){
  var post="name=王力猛&email=wallimn@sohu.com&bokee=http://down.111cn.net";
  post = encodeuri(post);
  post = encodeuri(post);//兩次,很關鍵
  var xmlobj = new activexobject('msxml2.xmlhttp');
  var url = '<%= path%>/page/act.jsp';//檔案名稱需要調整成測試時的相應位置?
  xmlobj.open ('post',url,true);
  xmlobj.setrequestheader("cache-control","no-cache");
  xmlobj.setrequestheader("content-type","application/x-www-form-urlencoded");
  xmlobj.send (post);//注意:post方式,使用這個來發送內容?
   }
   </script>
  </head> 
  <body>
  <input type="button" value="提交" onclick="justdo()"/>
   </body>
</html>

/////////////////////////////////////////////////////////////////////////////////////
  ajax請求處理頁面(act.jsp)的內容如下:
/////////////////////////////////////////////////////////////////////////////////////

<%@ page language="java" import="java.util.*" pageencoding="gb18030"%>
<%string path = request.getcontextpath();%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<%@page import="java.net.urldecoder"%>
<html>
  <head>
    <title>ajax deal</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
  </head>
  <body>
  <%
  //遍曆輸出參數內容。
  for (enumeration e = request.getparameternames(); e.hasmoreelements();) {
   string h = (string) e.nextelement();
   string v = request.getparameter(h);
   string mm =  java.net.urldecoder.decode(v, "utf-8");
   system.out.println("請求參數: " + h + " = " + mm);
  }
   %>
  </body>
</html>

/////////////////////////////////////////////////////////////////////////////////////

相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。