用struts1的后台控制,代码如下
response.setContentType("text/json");

int pages = Integer.valueOf(request.getParameter("page"));
int rp = Integer.valueOf(request.getParameter("rp"));
String sortname = request.getParameter("orderid");

List rows =orderDao.searchDetails((pages - 1) * rp, rp, sortname);
int total = rows.size() ;


JSONObject jsonObj = new JSONObject();

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");


jsonObj.put("page", pages) ;
jsonObj.put("total", total) ;
jsonObj.put("rp", rp);
jsonObj.element("rows", rows) ;




response.getWriter().write(jsonObj.toString()) ;
System.out.println(jsonObj.toString());

return null;
在控制台打印有数据,但是write到页面全部是undefined...

解决方案 »

  1.   

    {"page":1,"total":3,"rp":10,"rows":[["1",10,1,null],["1",0,3,null],["1",0,2,null]]}这是控制台打印得到的结果,页面显示为undefined貌似是这个输出格式有问题?以前正常的都是{"page":1,"total":3,"rp":10,"rows":[{"1",10,1,null},{"1",0,3,null},{"1",0,2,null}]}用的是json包的put 或者 element 结果都一样……
      

  2.   

    {"1",0,3,null}
    这种格式也不是json啊
    但是write到页面全部是undefined... ?
    这个页面的源代码是什么样子的?
      

  3.   


    这个问题解决了,现在又遇到一个问题,我想用ajax批量提交数据http://localhost:8080/Test1/jsp/example.jsp?det[0].item=1&det[0].price=10&det[0].quantity=1&det[1].item=1&det[1].price=2.33&det[1].quantity=3&det[2].item=1&det[2].price=5.5&det[2].quantity=2现在按了提交可以得到这样的一串url,我用的是struts1,该怎么取出这些数据呢
      

  4.   

    你可以把这串参数用json的形式提交,然后后台获取这个json字符串,转成json就可以了var json = JSON.encode(det);
    "http://localhost:8080/Test1/jsp/example.jsp?jsonstr="+jsonJSON.encode这个是我自己随便写的,反正就是一个json转化的函数,你可以去找一下,jquery有插件,