jquery ajax Web Effects p/jsp.html target=_blank >jsp Tutorial return value garbled two-way solution in the previous development of Ajax often encounter garbled, today we use jquery ajax+jsp, and out of garbled, Regular test found two kinds of processing jquery ajax garbled problem, see below.
<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 tutorials 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 ();
}
}
/*
The return value is all garbled
Solution One
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%>
Solve Ajax Scrambling method Two, add a filter
*/
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";//This is consistent with your 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;
}
}
}
%>