我使用jquery进行ajax验证,在action里面返回一个JSONObject对象。代码如下:List temp = packageService.queryCardBySeq(cardseq);

JSONObject jsonObj = new JSONObject();
if(temp!=null&&temp.size()>0){
Card card = (Card)temp.get(0);
jsonObj.accumulate("id", card.getId());
jsonObj.accumulate("card_code", card.getCard_code());
jsonObj.accumulate("card_name", card.getCard_name());
jsonObj.accumulate("card_seq", card.getCard_seq());
jsonObj.accumulate("card_value", card.getCard_value());
jsonObj.accumulate("tel_no", card.getTel_no());
out.print(jsonObj);
                            }页面上取msg.card_name时乱码,其他都正确(其他不含中文只有card_name含中文)。搞了一早上还是没能解决。该用的方法都用了。
response设置为utf-8,页面设置成utf-8,容器设成utf-8全都试过了还不行。有没有谁知道如何解决。尽量说点有实际作用的。对了下面是jsp请求         $.ajax({url: "${home}/sales/packagemg.do?cmd=checkCardIsAxist",
type:"post",
dataType:"json",
cache:false,
data:"cardseq="+obj.value,
success:function(msg){
if(msg==0){//返回的是0的话代表该串号不存在所以不能打包
alert("该卡不能打包!");
tempTd[0].children[0].value="";
tempTd[0].children[1].value="";
tempTd[2].children[0].value="";
tempTd[3].children[0].value="";
tempTd[4].children[0].value="";
obj.focus();
}else{//否则的话代表串号存在,则把返回的对象的各个属性加载到文本框中
tempTd[0].children[0].value=msg.card_code;
tempTd[0].children[1].value=msg.id;
tempTd[1].children[0].value=msg.card_seq;
tempTd[2].children[0].value=msg.tel_no;
tempTd[3].children[0].value=msg.card_name;
tempTd[4].children[0].value=msg.card_value;
}
}
});

解决方案 »

  1.   

    肯定是服务端输出的json字符编码和客户端接收时页面的编码不一致造成的。你的action代码没给全,out 哪儿来的? 贴全点呢,这样看不出问题,最好还把jsp的头部贴上来。
      

  2.   

    整合时越到如下错误 有的说是版本不对 可我把 jsonplugin-0.32 jsonplugin-0.33 jsonplugin-0.34都换过了 还是不行的啊 请高手指点
    Servlet.service() for servlet default threw exception
    java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.get(Ljava/lang/String;)Ljava/lang/Object;
    at com.googlecode.jsonplugin.JSONResult.execute(JSONResult.java:159)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept
      

  3.   

    jsonObj.accumulate("card_name", card.getCard_name());
    在这段代码时card.getCard_name()是乱码吗?要是,有可能是数据库的问题,要不是就要转码
    用UTF-8编码,用ISO-8859-1解码,然后再到前台看看是不是乱码,要是的话再处理下,没用过
    JSON,不过原理应该一样
      

  4.   

    在action里把你取到的数据打印出来看下,是不是从数据库中取到的就是乱码,如果是,统一数据库和程序的编码,如果不是,那问题就可能出在程序和页面之间了。JSON只是传输数据的一个桥梁,乱码跟JSON应该没关系。
      

  5.   

    ......我不是说了action里面读出来没问题,就是页面取的有问题,页面的头没错,我设置编码为utf-8了。
    <%@ page language="java" contentType="text/html; charset=utf-8"%>原理不要讲了,我都懂。能否讲点能实际解决问题的办法
      

  6.   

    顺便说下一楼兄弟的疑问,我的out没有问题,通过PrintWriter out= response.getWriter();得到的。
    再次说明一下,数据从库里读出来没问题,仅仅是页面上取值时乱码了。页面编码格式,response返回的编码方式都改了。麻烦各位讲点实际能解决问题的方法。
      

  7.   

    你既然啥都懂了,还出错?用fiddler分析下服务器返回到客户端的数据。
      

  8.   

    晕,兄弟不是我不虚心,只是确实急而且比较棘手的问题。楼上的回复非常感谢,但是楼上的讲的确实都没实际意义。
    你说的用fiddler分析能否讲明白点
      

  9.   

    fiddler是个工具。可以帮助你查看http response和request的真实内容。
    用firefox的插件firebug也可以。
    你既然不知道问题出在哪里,那就反证,确认每个细节都没问题。HTTP/1.1 200 OK
    Cache-Control: private
    Content-Type: text/html; charset=utf-8
    Vary: Accept-Encoding
    Server: Microsoft-IIS/7.0
    X-AspNet-Version: 2.0.50727
    Set-Cookie: AreYouHuman=7981; domain=.blogjava.net; path=/
    X-Powered-By: ASP.NET
    Date: Fri, 25 Dec 2009 05:05:26 GMT
    Content-Length: 118608
      

  10.   

    用火狐浏览器 firebug~看下  取的值是什么~~~~
      

  11.   

    服务器是apache tomcat吗?  如果是的话在tomcat的安装目录下的conf目录下的server.xml文件中的<Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443" /> 里面加上URIEncoding="UTF-8"就行了
      

  12.   

    去掉contentType:"application/json;charset=utf-8",response.setContentType("text/html;charset=GBK");
    设置下,我这样设置解决了