前端向后端传值乱码?
(1)如果是get请求改成post请求试试,数据最好写在data:{ };
(2)客户端用js函数encodeURI()对中文字符进行两次编码,服务器端采用URLDecoder类对客户端传输过来的中文字符进行UTF-8格式的解码。
data:“name=”+encodeURI(encodeURI(“张三”)),
String name = URLDecoder.decode(“客户端传输过来的中文字符”,“UTF-8”);
(3)ajax配置contentType属性,加上charset=UTF-8 ,默认jQuery里的contentType是没有的
在ajax方法中加入以下参数 contentType: “application/x-www-form-urlencoded; charset=UTF-8″
使用其它js框架或者xhr都是差不多,设置请求头Request Headers中contentType即可,
后端向前端传值乱码?
(1) response设置编码格式
response.setCharacterEncoding("UTF-8");
response.setHeader("contentType", "text/html; charset=utf-8");
或者一行代码搞定:response.setContentType("text/html; charset=utf-8");