zoukankan      html  css  js  c++  java
  • 使用ajax请求SpringMVC返回Json出现乱码解决方法

    1:在使用ajax请求后台访问数据的数据,后台返回的数据是乱码,带??问号的乱码,之前还一直没有遇到过,在这里记录整理一下,贴出解决代码! 
    (1):前台使用ajax ,已经设定返回的结果为json格式!ajax代码不贴出来了! 
    (2):后台代码

    @RequestMapping(value = { "/hello/{uuid}" }, method = RequestMethod.GET /*,produces = "text/html;charset=UTF-8"*/)
        @ResponseBody
        public String hello(@PathVariable("uuid") String uuid) {
            String result = "";
    
            //do something  
            //使用Json返回json格式数据
    
            return JSON.toJSONString(result);;
        }

    在没有加produces = “text/html;charset=UTF-8” 之前,返回的结果一直是乱码,很奇怪,项目中web.xml也设置了编码格式utf-8 ,没有找到最终的原因,不过找到了这种解决方法!

    2:如果上面的方法还是不能解决的话就用下面的方法:

    @ResponseBody
        public void hello(@PathVariable("uuid") String uuid,HttpServletResponse response) {
            String result = "";
    
            //do something  
            //使用Json返回json格式数据
            JSONObject josn = new JSONObject();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(JSON.toJSON(result));
        }
  • 相关阅读:
    用户 'IIS APPPOOLPrivate' 登录失败。
    WCF 内置跟踪日志
    Ionic3 下拉刷新
    Ionic3 组件懒加载
    Ionic3 编程-应用启动进入引导页
    mysql数据库导入导出
    PHP-生成二维码(qr-code)
    javascript 总结(常用工具类的封装)
    Ajax跨域原理及解决方案
    封装获取连续数字的拼接
  • 原文地址:https://www.cnblogs.com/xuyuanjia/p/5859000.html
Copyright © 2011-2022 走看看