jquery ajax post返回的中文值是乱码

J2EE 码拜 9年前 (2015-04-04) 1196次浏览 0个评论
 

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会变成乱码。还是问号??

jquery ajax post返回的中文值是乱码
后台先打个断点,确定后台不是乱码, 然后看前台页面的编码格式
jquery ajax post返回的中文值是乱码
后台查询的时候返回的值不是乱码 然后在js里data就变成乱码了
jquery ajax post返回的中文值是乱码
引用 2 楼  的回复:

后台查询的时候返回的值不是乱码 然后在js里data就变成乱码了

那就是你前后台编码不一样造成乱码了.

jquery ajax post返回的中文值是乱码
前后台编码不一样
jquery ajax post返回的中文值是乱码
10分
String userName = request.getParameter(“userName”);
你看看这个时候的userName是不是乱码,如果是这样试试

String userName= new String(request.getParameter("userName").getBytes("ISO-8859-1"),("UTF8"));
jquery ajax post返回的中文值是乱码
20分
 //发送邮箱
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 请求方式!在后台返回的是数字,也可是汉字!

jquery ajax post返回的中文值是乱码
10分
response.setContentType(“application/xml;utf-8”);
response.setCharacterEncoding(“utf-8”);
jquery ajax post返回的中文值是乱码
该问题已解决 谢谢大家
原因是我用了@ResponseBody注解把返回值直接写到HTTP response body里,spring注解的默认编码是iso-8859-1,我的工程编码和页面都是utf-8,所以乱码
在springmvc的配置文件中加入
<!– 启动Spring MVC的注解功能,完成请求和注解POJO的映射 –>    
<bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter” >  
<property name=”messageConverters”>   
         <list>   
             <bean class = “org.springframework.http.converter.StringHttpMessageConverter”>   
                <property name = “supportedMediaTypes”>
                      <list>
                          <value>text/html;charset=UTF-8</value>   
                     </list>   
                </property>   
             </bean>   
         </list>   
   </property>  
</bean>  
就可以了  :)
jquery ajax post返回的中文值是乱码
[color=#FFFF00][/color]

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明jquery ajax post返回的中文值是乱码
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!