ajax json 处理返回值方法,附带中文乱码解决方案
来源:互联网 发布:蓝月传奇官印数据 编辑:程序博客网 时间:2024/05/21 09:45
java中转换成json对象方法:
public JSONObject toJsons(Object o) {
//HttpServletResponse response = (HttpServletResponse)ServletActionContext.getResponse();ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
JsonConfig jsonConfig = new JsonConfig();
JSONObject jo = JSONObject.fromObject(o,jsonConfig);
return jo;
}
此处;一定加上转换编码:并且得在out前加;
response.setContentType("application/json;charset=utf-8");
PrintWriter out=response.getWriter();
如果用 $.ajax({
type : "get",
url : "actionname.action?pc=+"+pid+"&sc="+sid+"&flag="+flag,
async : false,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success : function(data) {
$("#mainajax").html(data);
}
});
方式传值,调用的是jquery里面的方法,pid,sid等内容是中文时,后台有可能解析成乱码。此时,只需要后台如此处理:pid.getBytes("ISO-8859-1"),"utf-8")。因为jquery中默认的contentype字符集是ISO-8859-1。如果不加红色字体部分,那么浏览器的不同会导致中文汉字解析异常。ie中后台需写:pid.getBytes("ISO-8859-1"),"gb2312")。火狐,谷歌,360,sara等需写:pid.getBytes("ISO-8859-1"),"utf-8")。
0 0
- ajax json 处理返回值方法,附带中文乱码解决方案
- ajax返回乱码处理方法
- @ResponseBody 返回json 中文乱码解决方案。
- SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案
- SpringMVC+jQuery+json返回中文乱码处理
- ajax返回中文乱码
- ajax返回中文乱码
- Ajax中文乱码解决方案
- ajax中文乱码解决方案
- Ajax中文乱码解决方案
- ajax中文乱码解决方案
- ajax中文乱码解决方案
- Ajax中文乱码解决方案
- Ajax中文乱码解决方案
- ajax调用,action返回的中文为乱码的解决方案
- Ajax 返回值 中文乱码 解决
- Ajax返回值--中文乱码解决
- Ajax/Json乱码处理
- 1+1/2+1/3+1/4+.....+1/20的和
- AFNetworking 与ASIHTTPRequest的比较
- hibernate的注解属性mappedBy详解
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- VC++中用API调用对话框资源
- ajax json 处理返回值方法,附带中文乱码解决方案
- boj429.学姐的数码管-输出格式以及代码风格的考量
- asp.net一个已实现的登陆过滤器
- ARM学习笔记(二)——ARM数据处理指令、加载\存储指令
- UVA - 10273 Eat or Not to Eat?
- hdu1312Red and Black(深搜)
- 阿里云Linux系统挂载数据盘
- 03 Array
- IconFont浅谈