日期:2014-05-17  浏览次数:20758 次

jquery ajax 返回值 在页面上是乱码
JScript code

//加载新闻栏目    
$().ready(function(){
var url="/admin/ajaxGetNewsClassFather.do";
var pars="random="+Math.random();
var newsClassFather= $('#newsClassFather');
        $.ajax({
            url : url,
            data : pars,
            type :"POST",
            success :function(res){
                newsClassFather.append(res);
            }
        });
    });


Java code

@RequestMapping("/admin/ajaxGetNewsClassFather.do")
public void ajaxGetNewsClassFather(Writer writer, Model model, HttpServletRequest 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();
        }
    }




在页面显示全是
??????
??????
??????
??????
??????
??????
??????
??????
??????

------解决方案--------------------
郁闷,昨天搞了一天解决,今天早上加上这句response.setContentType("text/html;charset=gbk"); 就ok了