首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jquery + ajax 在服務器端使用json格式怎麼傳送數據?该怎么解决

2012-02-10 
jquery + ajax 在服務器端使用json格式怎麼傳送數據?求具體代碼還有在客戶端 怎麼解析。感謝[解决办法]Java

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就是你的数据,干你想干的事情都在这里。    }}); 

热点排行