如果页面和后台,全部用统一的utf-8编码,就没必要看下文了 。
如果 页面上是 gbk,或是 其他的编码,如下方式 ,可以消除编码问题,如下所示 :
<meta http-equiv="content-type" content="text/html; charset=gbk" />
<script>
function updateComments(pn,articleid){
var url="comment.jsp?pn="+pn+"&articleid="+articleid+"&act=query";
var myAjax = new Ajax.Updater('comments',url, {method: 'get',evalScripts: true});
}
function postData() {
var url = "comment.jsp";
var form=document.forms["theform"];
$("submit").disabled=true;
var myAjax = new Ajax.Request(
url,
{
method: 'post',
parameters: encodeURI( Form.serialize(form) ),
onComplete: showResponse
});
return false;
}
function showResponse(response) {
var txt=response.responseText;
alert(txt);
if(txt=="0") {
alert("提交成功!");
$("submit").disabled=false;
}
else if(txt=="1"){
}
}
</script>
文章回复:
<form method="post" name="theform" action="comment.jsp">
<input type="hidden" value="<%=o.getArticleid()%>" name="articleid" />
<input type="hidden" value="add" name="act" />
<textarea id="comment" name="comment" cols="60" rows="8" wrap="virtual" class="db7"></textarea><br/>
<input type="button" value="submit" name="submit" id="submit" οnclick="postData();" />
</form>
后台应该转变一下编码,就可以消除乱码:如下所示:
String comment = request.getParameter("comment");
comment = java.net.URLDecoder.decode(comment,"utf-8");
comment = new String(comment.getBytes("GBK"),"ISO-8859-1");