日期:2014-05-16  浏览次数:20633 次

关于ajax从数据库取数据向前台传的问题 - Web 开发 / Ajax
//后台Java   servlet中将从数据库中数据封装成XML
    PrintWriter   out   =   response.getWriter();

    response.setContentType( "text/xml;charset=GBK ");
    response.setHeader( "Cache-Control ",   "no-cache ");

    out.println( " <response> ");

    Iterator   iter   =   allRoom.iterator();
    while   (iter.hasNext())   {
name   =   (String)   iter.next();
out.println( " <room> "   +   name   +   " </room> ");
    }
//   获取信息
    BookinfoT   info   =   (BookinfoT)   bookInfo.get(0);
    SimpleDateFormat   sdf   =   new   SimpleDateFormat( "HH:mm ");
    for   (int   i   =   0;   i   <   bookInfo.size();   i++)   {
              out.println( " <time> "   +   sdf.format(info.getBkStarttime())
+ "- "+sdf.format(info.getBkEndtime())+   " </time ");
              out.println( " <purpose> "   +   info.getBkPurpose()   +   " </purpose> ");
    }
//   获取上午会议室
    iter   =   amUnuseroom.iterator();
    while   (iter.hasNext())   {
name   =   (String)   iter.next();
out.println( " <amUnusedRoom> "   +   name   +   " </amUnusedRoom> ");
    }
    iter   =   pmUnuserroom.iterator();
    //   获取下午会议室
    while   (iter.hasNext())   {
name   =   (String)   iter.next();
out.println( " <pmUnusedRoom> "   +   name   +   " </pmUnusedRoom> ");
    }

    out.println( " </response> ");

    allRoom   =   null;
    bookInfo   =   null;
    amUnuseroom   =   null;
    pmUnuserroom   =   null;
    service   =   null;
    out.close();

//前台接收代码
var   length=XMLData.getElementsByTagName( "time ").length;
length=XMLData.getElementsByTagName( "amUnusedRoom ").length;
length=XMLData.getElementsByTagName( "pmUnusedRoom ").length;

//请问为什么三个length的值都为0?


------解决方案--------------------
看一下xmlHttp.responseXML是什么东西,如果只有 <response> </response> 那就是后台问题,没有数据
out要刷新下 out.flush()