购物车终于算是前后端跑通了,总结一下。

前端: ajax异步提交。

$.get("GoodsActionForUser?flag=getGoodsesByIds",JSON.parse(params),function(r){
            alert(r);
        });

三个参数分别为url,json,callback...开始写成了$.getJSON(),这个函数就是已经给你json.parse()了,所以导致后边alert出现object Object

后端:servlet处理。

 

 1 if("getGoodsesByIds".equals(flag)){  //获取商品列表
 2             response.setCharacterEncoding("UTF-8");
 3             //response.setContentType("application/json");
 4              
 5             //goodsIds = request.getParameter(goodsIds);
 6             System.out.println("getGoodsesByIds   "+goodsIds);  //用servlet的doGet接收。
 7             String[] ids=goodsIds.split(",");
 8             goodses = goodsService.getGoodsesByIds(ids);
 9             for(Goods goods:goodses){
10                 System.out.println(goods.getGname());
11             }
12             JsonConfig c = new JsonConfig();
13             c.setExcludes(new String[] {"category","goodsNo","cid","price1","stock","description" });
14             JSONArray a = JSONArray.fromObject(goodses,c);
15             //result = a.toString();
16             result = a.toString();
17             System.out.println(result);
18             PrintWriter out = response.getWriter();
19             out.print(result);//print 可以以二进制形式输出对象,write输出数据,数组
20             out.flush();
21             out.close();      //这一块红色代码来实现向前端传数据!
22         }

 

在处理json时需要用到一些相关的jar包,这个百度一下就可以。

 

相关文章:

  • 2022-01-14
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2022-02-20
  • 2022-12-23
猜你喜欢
  • 2021-12-27
  • 2021-11-07
  • 2022-12-23
  • 2021-12-05
  • 2021-12-19
  • 2021-06-14
  • 2021-11-08
相关资源
相似解决方案