jquery ajax post返回的中文值是乱码?
var userName = $(this).attr("value");
$('#fnamealert').addClass('alert');
$('#fnamealert').html("正在查找中...");
$.post("useraction/findquestion.do","userName="+userName,function(data){
if(data!=""){$('#fquestion').val(data);$('#fnamealert').html("完毕")}else{$('#fnamealert').html("查找出错 - -!");$('#fquestion').val("");};
});
这里的data返回的时候就成了乱码 请求的方法是
@ResponseBody
@RequestMapping(value="findquestion")
public String findQuestion(){
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
String userName = request.getParameter("userName");
User user = userservice.findbyusername(userName);
if(user != null){
return (String)user.getQuestion();
}
else{
return "";
}
}
咋办呢 我数据库 环境 页面都是用的utf-8 一般的传递没问题 但是到了这个post会变成乱码。还是问号??
------解决方案--------------------String userName = request.getParameter("userName");
你看看这个时候的userName是不是乱码,如果是这样试试
Java code
String userName= new String(request.getParameter("userName").getBytes("ISO-8859-1"),("UTF8"));
------解决方案--------------------
//发送邮箱
function functclickpi(comcode,answer,email){
$.post(
"<%=path%>/lms/comuser!sendMsg.so",
{comcode:comcode,answer:answer,email:email},
function (msg){
if("1"==msg){
alert("密码已发送到你的邮箱,请去验收!");
window.close();
return false;
}else if ("2"==msg) {
alert("答案有误,请重新输入!");
return false;
}
},
"json"
);
}
你试试这种ajax 请求方式!在后台返回的是数字,也可是汉字!
------解决方案--------------------
response.setContentType("application/xml;utf-8");
response.setCharacterEncoding("utf-8");