解决乱码问题
-
-Dfile.encoding=UTF-8 //服务器配置设置 VM—options 哪里设置
-
5.配置tomcat
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
-
response.setCharacterEncoding("UTF-8");
-
当返回值为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"})
-
$(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) } }) }) })
-
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>
-
request.setCharacterEncoding("utf-8");
8.
-
city_name = new String(jsonStr.getBytes("ISO-8859-1"), "UTF-8");//后台接到前端的值是,可以尝试设置一下
-
用ajax请求的,报错原因编码问题,加入contentType : 'application/json',就可以了
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 前端设置utf-8字符编码