读书人

jquery + ajax 在服器端使用json格式

发布时间: 2012-02-10 21:27:42 作者: rapoo

jquery + ajax 在服器端使用json格式怎送?
求具代

有在客端 怎解析。感

[解决办法]

Java code
List<Map<String, String>> list = new ArrayList<Map<String, String>>();    int total = dpDirectoryService.getDpDirectoryCount(dpDirectory);    list = dpDirectoryService.getDpDirectoryInfo(dpDirectory);    PrintWriter write = response.getWriter();    JSONObject o = new JSONObject();    o.put("total", total);    o.put("rows", list);    [color=#FF0000]write.write(o.toString());[/color]    write.flush();    write.close();    o.clear();
[解决办法]
服务器端你用什么语言,java你可以用fastjson直接解析一个对象,返回JSON字符串,然后用response对象
输出他。
php直接用echo json_encode(array('name'=>'吴红军'));这样输出返回给客户端

jquery ajax这样获取:
JScript code
$.ajax({    url:'服务器端地址',    type:'GET',//以GET方式请求,    data:{id:1},//你需要传递的数据,我这里id:1随便写的    dataType:'json',//如果放回json,这里必须明确表示为json,否则他不理你的,    success:function(rs){        rs.xxx//xxx就是你的数据,干你想干的事情都在这里。    }}); 

读书人网 >Java Web开发

热点推荐