Ajax 返回值 中文乱码 解决

/**

*使用ajax检查用户是否存在

*/

publicvoidcheckUser(){

try{

System.out.println("username="+user.getUserName());

HttpServletResponseresponse=ServletActionContext.getResponse();

response.setContentType("text/xml;charset=utf-8");

response.setHeader("Cache-Control","no-cache");

PrintWriterpw=response.getWriter();//输出中文,这一句一定要放到response.setContentType("text/xml;charset=utf-8"),response.setHeader("Cache-Control","no-cache")后面,否则中文返回到页面是乱码

if(existUser.contains(user.getUserName()))

{

pw.print("存在");

}else{

pw.print("不存在");

}

pw.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

前台:

$.ajax({

type:"POST",

url:"${contextPath?if_exists}/resource/cascade.action",

data:{firstType:filrstTypevl},

contentType:"application/x-www-form-urlencoded;charset=utf-8",

dataType:"json",

success:function(resp){

$("#secondTypeoption").remove();

$("#secondType").append("<optionvalue=''>--请选择二级分类--</option>");

alert(resp);

$.each(resp,function(i){

$("#secondType").append("<optionvalue="+resp[i].kdDetail+">"+resp[i].kdDesc+"</option>");

});

}

});

相关推荐