史上最全编码设置大全

解决乱码问题

  1. -Dfile.encoding=UTF-8 //服务器配置设置 VM—options 哪里设置

  2. 5.配置tomcat

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>
    
  3.  response.setCharacterEncoding("UTF-8");
    
  4.   当返回值为json类型时,为      produces = "application/json; charset=utf-8"
    
    当返回值为String类型时,为   produces = "text/plain;charset=utf-8"
        示例:
      @RequestMapping(value = "getName",method = RequestMethod.POST,produces = {"text/plain;charset=utf8","text/html;charset=utf-8"})
    
  5.   $(function (){
            var obj={"newsid":"4","newstitle":"舒豪","newscontent":"666"}
            var str="<table> <tr><td>新闻id</td><td>新闻标题</td><td>新闻内容</td></tr>"
            $("#btn").click(function (){
                $.ajax({
                    type:"POST",
                    url:"/getOne",
                    data:JSON.stringify(obj),
                    dataType:"json",
                   // contentType:'application/json;charset=utf-8',
                   // data:JSON.stringify(obj),//对象转json字符串,要么这么						    写'{"userid":"111","username":"222","pwd":"333","status":"444"}'
                    contentType:'application/json;charset=utf-8',//前端传入的数据类型指定
                    success:function (data){
                        str+="<tr><td>"+data.newsid+"</td><td>"+data.newstitle+"</td><td>"+data.newscontent+"</td></tr>"
                        str+="</table>"
                        $("#box").html(str)
                    }
                })
            })
        })
    
  6. web.xml中配置
    <!-- 解决post乱码问题 -->
      <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 设置编码参是UTF8 -->
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    
  7. request.setCharacterEncoding("utf-8");
    

    8.在这里插入图片描述

  8. city_name = new String(jsonStr.getBytes("ISO-8859-1"), "UTF-8");//后台接到前端的值是,可以尝试设置一下
    
  9. 用ajax请求的,报错原因编码问题,加入contentType : 'application/json',就可以了
    
  10.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 前端设置utf-8字符编码
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值