<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
jsp页面调用Ajax 请求,后台中文解析乱码
前台页面↓↓↓
请求信息↓↓↓
后台接受的数据↓↓↓
测试不行的方法入下
request.setEncoding() ----不行
new String(request.getParameter("").getBytes(),"GBK");----不行
contentType: "application/x-www-form-urlencoded; charset=GBK", ----不行
contentType: "application/x-www-form-urlencoded; charset=UTF-8", ----不行
最终的解决方案两步
1.将传入的参数进行encodeURI编码两次(这里我值只对中问参数进行了编码)
var params = "type=1&objectNo=<%=buyerNo%>&userId=<%=bizuserId%>&opinion="+encodeURI(encodeURI($("#buyerEvaluateText").val()));
var url = "<%=request.getContextPath()%>/Domestic/Evaluate.do?method=addEvaluate";
$.post(url,params,function(data){
if(data == "1"){
alert("保存成功!");
}
});
2.后台进行解码
String opinion = request.getParameter("opinion");
opinion = URLDecoder.decode(opinion, "UTF-8");
参考:http://www.jb51.net/article/31187.htm