日期:2014-05-17  浏览次数:20713 次

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