jquery ajax 網頁特效p/jsp.html target=_blank >jsp教程 傳回值亂碼二向解決方案在以前開發ajax時經常會碰到亂碼,今天我們用jquery ajax+jsp時,又出來亂碼了,經常測試找到了二種處理jquery ajax亂碼問題,下面看看。
<script>
$().ready(function(){
var url="/www.111cn.net/ajaxgetnewsclassfather.do";
var pars="random="+math.random();
var newsclassfather= $('#newsclassfather');
$.ajax({
url : url,
data : pars,
type :"post",
success :function(res){
newsclassfather.append(res);
}
});
});
</script>
<%
//ajaxgetnewsclassfather.do
public void ajaxgetnewsclassfather(writer writer, model model, https教程ervletrequest request, httpservletresponse response)
{
response.setcontenttype("text/html");
response.setcharacterencoding("utf-8");
string dbname= cookieutil.getdbname(request, response);
list<newsclass> newsclasslist= newsclassjdbcservice.findbybclassid(dbname,0);
string result="";for (newsclass newsclass : newsclasslist)
{
result+="<option value='"+ newsclass.getclassid()+"'>"+ newsclass.getclassname()+"</option>";
}try
{
writer.write(result);
}catch (ioexception e)
{
e.printstacktrace();
}
}
/*
傳回值全部為亂碼
解決方案一
<%@ page language="java" contenttype="text/html; charset=utf-8"%>
解決ajax亂方法二,加個過濾器
*/
package com.zlm.tools;
import java.io.ioexception;
import javax.servlet.filterchain;
import javax.servlet.filterconfig;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
public class tounicode implements javax.servlet.filter {
private string tounicode = "iso-8859-1";//這個和你web 裡傳過來的一致;
public void destroy() {
}
public void dofilter(servletrequest request, servletresponse response,
filterchain chain) throws ioexception, servletexception {
request.setcharacterencoding(this.tounicode);
response.setcharacterencoding(this.tounicode);
chain.dofilter(request, response);
}
public void init(filterconfig filterconfig) throws servletexception {
string unicode = filterconfig.getinitparameter("tounicode");
if (unicode != null && unicode.length() > 0) {
this.tounicode = unicode;
}
}
}
%>