前台ajax(jsp文件):
(1)
$.ajax({
type: "POST",
url: "/dragable/demo/finishTopo",
async:true,
data: {json: "testDiv",name:"json"},
dataType: "json",
// dataType: "text",
success: function(data){
alert(data.list + data.total);
},
error: function(e) {
alert(e);
}
});
(2)
$.ajax({
type: "POST",
url: "/dragable/demo/finishTopo",
async:true,
data: {json: "testDiv",name:"json"},
//dataType: "json",
dataType: "text",
success: function(data){
data = data.replace(/'/g, '"');//将所有'替换成"
var jspare = JSON.parse(data);
alert(jspare.root[0].value);
},
error: function(e) {
alert(e);
}
});
后台(java文件):
(1)
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); JSONObject json1; Map<String, Object> map = new HashMap<String, Object>(); map.put("total", 56); map.put("list", "listjipa"); json1 = JSONObject.fromObject(map); out.print(json1.toString()); out.close(); }
(2)
StringBuilder str = new StringBuilder(); str.append("{'root':[{'name':6101,'value':'北京市'}, {'name':6201,'value':'福州市'},{'name':6102,'value':'天津市'}]}"); out.print(str.toString()); out.close();