(java)jsp页面ajax发出请求返回json格式数据 中文乱码问题
后台代码:Map<String,String>items=newHashMap<String,String>();items.put("name","李连杰");item...
后台代码:
Map<String, String> items = new HashMap<String,String>();
items.put("name", "李连杰");
items.put("sex", "男");
JSONObject jo = JSONObject.fromObject(items);
try {
PrintWriter out = response.getWriter();
response.setContentType("text/json; charset=UTF-8");
out.write(jo.toString());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
前台代码:
$.ajax({
type : "post",
url : "salary/queryFormulaValueById.action",
data : "formulaID=" + formula,
dataType:"json",
success : function(items) {
alert(items.name);
},
beforeSend : function() {
$("#formulamsg").html("查询中...");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("内部错误,请重新操作!");
}
});
页面头信息:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
success函数里面得到的items.name老是乱码(全是问号),实在无法解决,哪位大神能解决?不胜感激!! 展开
Map<String, String> items = new HashMap<String,String>();
items.put("name", "李连杰");
items.put("sex", "男");
JSONObject jo = JSONObject.fromObject(items);
try {
PrintWriter out = response.getWriter();
response.setContentType("text/json; charset=UTF-8");
out.write(jo.toString());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
前台代码:
$.ajax({
type : "post",
url : "salary/queryFormulaValueById.action",
data : "formulaID=" + formula,
dataType:"json",
success : function(items) {
alert(items.name);
},
beforeSend : function() {
$("#formulamsg").html("查询中...");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("内部错误,请重新操作!");
}
});
页面头信息:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
success函数里面得到的items.name老是乱码(全是问号),实在无法解决,哪位大神能解决?不胜感激!! 展开
4个回答
展开全部
setContentType要放在PrintWrite前面,你颠倒了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加个
$.ajax({
contentType : "application/json",});
$.ajax({
contentType : "application/json",});
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
response.setContentType()语句放在hashMap前面试试看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询