關於AJAX使用過程中的亂碼問題(討論)

下面是一段jsp頁面的ajax代碼,用的是(prototype.js)
     function ok(string)
       {
       //得到文本框值
    var description=document.getElementById("discriptionexpense1");
      //局部請求地址
    var url ="addexpenseand.action?description="+description.value;
     
      //使用prototype函數構造xmlhttprequest對象
      var myAjax = new Ajax.Request(
      url,
      {
          //請求方法爲post
          method:'post',
          //設置參數爲 string=string
          parameters:"string="+string,
          //設置回調函數
          onComplete:showResponse6,
          //是否異步
          asynchronous:true
      }
      );
      
      }
      
      function showResponse6(xmlrequest){
      //回調函數
      var text = xmlrequest.responseText;
      document.getElementById("formdiv").innerHTML=text;
       //將匹配的內容輸出到 span 層
     //$("formdiv").innerHTML=xmlrequest.responseText;
          
     //  document.location.reload();  
          }
jsp的代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
</head>
<body>
<input name="discriptionexpense" id="discriptionexpense" type="text" />
<div id="formdiv">
</div>
<input type='button' name='button' id="buttonadd"  value='Add' onClick="ok('add')"/>
    </body>
</html>

上面代碼中通過ajax向action傳了兩個參數,一個是字符串“add”,一個是input的輸入的值,利用webwork屬性在action得到這兩個參數卻得到亂碼,
相當鬱悶,無賴下只好repuest取值,依然亂碼。。。。
具體代碼:
private String description = null;
private String string= null;
    public String getDescription() {
  return description;
    }

    public void setDescription(String description) {
  this.description = description;
    }
    public String getString() {
  return string;
    }

    public void setString(String string) {
  this.string= string;
    }
public void  addexpenseand(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
String string1 = request.getParameter("string");//從JSP頁面拿值
String description1= request.getParameter("description");//從JSP頁面拿值
System.out.println(description );
System.out.println(string);
System.out.println(description1);
System.out.println(string1);
}
後來在
addexpenseand()方法中做了代碼轉換方纔解決問題:不過轉換後的代碼只支持IE,具體代碼:
String des = new String(request.getParameter("description").getBytes("ISO8859_1"),"GB2312");
可j
avascript是使用UTF-8國際編碼,這裏又需要轉成GBK。。。
請各位高手發表發表高見。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章