第一个ajax程序


公司一个页面上select查询的功能,试着用ajax写

比较蛋疼的事json只能传递字符串类型的参数···不如用pageBean方便···


<script language="javascript">
        var couponType = '${couponType}';
        //二级联动 查询活动类型
        
        $("#couponType").children("option").on('click',function() {
            var couponType = document.getElementById("couponType");
            var aindex = couponType.selectedIndex;
            var uid = $("#userId").val();
            
            var aurl = '${pageContext.request.contextPath}/back/coupons_select.action';
            $.ajax({
                    async : false,
                    type : "post",
                    url : aurl,
                    data : {index : aindex,userId : uid},
                    success : function(data) {
                        $("#coupontabel tbody").html("");
                        var obj = eval('('+data+')');
                        var i =0;
                        var a = 0;//获得二级select选中的那个option的下标
                        $.each(obj, function(key, val) {
                            var str = $("<tr><td>"+val['id']+"</td><td>"+val['price']+"</td><td>$300</td><td>"+
                                    val['type']+"</td><td>"+val['need']+"</td><td>"+
                                    val['sdate']+"至"+val['edate']+"</td></tr>"
                                    )
                            $("#coupontabel").append(str);
                        });
                    }
            });
        });
        
</script>

后台:

public void select(){
		HttpServletResponse res = ServletActionContext.getResponse();
		res.reset();
	    res.setContentType("text/html;charset=utf-8");
	    int i=Integer.parseInt( httpReq.getParameter("index"));
	    Long l = Long.parseLong(httpReq.getParameter("userId"));
	    List<CouponMemberModel> couponList = new ArrayList<CouponMemberModel>();
	    HqlHelper hql = new HqlHelper(CouponMemberModel.class,"a");
	    if(i==1||i==2||i==3){
	    	hql.addCondition("a.isUsed=?",IsUsed.valueOf(i-1));
	    }
	    if(l!=null){
	    	hql.addCondition("a.member.memberId=?", l);
	    }
	    couponList = couponMemberModelServiceImpl.getAll(hql );
	    List<JsonTest> jsonList = new ArrayList<JsonTest>();
	    for(CouponMemberModel m:couponList){
	    	JsonTest j = new JsonTest();
		    j.setId(m.getCoupon().getCouponId().toString());
		    j.setPrice(m.getCoupon().getCouponMoney().toString());
		    j.setNeed(m.getCoupon().getCouponDetail());
		    j.setType(m.getCoupon().getCouponTypeId().getCouponType());
		    j.setLimit(m.getCoupon().getCouponMoney().toString());
		    j.setSdate(m.getCoupon().getCouponBeginTime().toString());
		    j.setEdate(m.getCoupon().getCouponEndTime().toString());
		    jsonList.add(j);
	    }
	    JSONArray jsonArray = JSONArray.fromObject(jsonList);
        
        PrintWriter pw;
	    
        try {
        	pw = res.getWriter();
        	pw.print(jsonArray);
        	pw.flush();
        	pw.close();
        } catch (IOException e) {
        	e.printStackTrace();
        }
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值