Ajax 调用restful服务返回json
发布时间:2020-12-16 03:21:30 所属栏目:百科 来源:网络整理
导读:$(function(){ init();})function init(){ myShopCar();//加载购物车信息}//我的购物车function myShopCar(){ var result = ""; var callType = "GET";//请求方式 var dataType = "json";//数据类型text xml json script jsonp param = { "userid":usid }; v
$(function(){ init(); }) function init(){ myShopCar();//加载购物车信息 } //我的购物车 function myShopCar(){ var result = ""; var callType = "GET";//请求方式 var dataType = "json";//数据类型text xml json script jsonp param = { "userid":usid }; var params = JSON.stringify(param); var userDetailUrl = rootPath+"webresources/products/queryMyShopCar?params="+params; if(""!= usid){ result = sendAjaxRequest(userDetailUrl,callType,dataType); setMyShopList(result.data); } } //我的购物车界面 function setMyShopList(objVal){ $("#shopCarId").html(""); shopHtml=""; shopHtml+='<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab02"> '; shopHtml+='<tr> '; shopHtml+='<td class="ta" style="width:8%;"><input id="allCheckBox" type="checkbox" value="product1" onclick="selectAll();" />全选</td> '; shopHtml+='<td class="ta" style="width:60%;">商品名称</td> '; shopHtml+='<td class="ta">单价</td> '; shopHtml+='<td class="ta">操作</td> '; shopHtml+='</tr> '; for(var i=0; i<objVal.length; i++){ shopHtml+='<tr> '; shopHtml+='<td><input id="'+objVal[i].productId+'" name="cartCheckBox" type="checkbox" value="product1" onclick="selectSingle()"></td> '; shopHtml+='<td ><img class="newsListsImg" src="'+rootPathImage+objVal[i].Imgurl+'"> '; shopHtml+='<a href="#" style="color:#1965B3">'+objVal[i].productName+'</a></td> '; shopHtml+='<td style="color:#FE6400">¥'+objVal[i].Productprice+'</td> '; shopHtml+="<td ><a href="javascript:delSelect('"+objVal[i].Shopcarid+"');" style='color:#1965B3'> 删除</a></td>"; shopHtml+='</tr> '; } shopHtml+='<tr > '; shopHtml+='<td colspan="4" style="text-align:center;"><input onclick="submit();" type="button" value="购买产品"/></td> '; shopHtml+='</tr> '; shopHtml+='</table> '; $("#shopCarId").append(shopHtml); } /** * 删除选择的购物车产品 * @param shopcarid */ function delSelect(shopcarid){ var result = ""; var callType = "GET";//请求方式 var dataType = "json";//数据类型text xml json script jsonp param = { "shopcarid":shopcarid }; var params = JSON.stringify(param); var delUrl=rootPath+'webresources/products/deleteMyShopCar?params='+params; if(""!= shopcarid){ result = sendAjaxRequest(delUrl,dataType); if(result){ alert("删除成功!"); myShopCar(); }else{ alert("删除失败!"); } } } //公共ajax方法,取得返回的整个JSON对象 /** * 公共ajax方法,取得返回的整个JSON对象 * * @param callUrl * 访问地址 * @param callType * 请求类型("get" or "post") * @param dataType * 请求的数据类型 * @returns 返回json数据,如后台返回的是{"SUCCESS": true,"DATA": * {"goodsName":"商品添加","price":"56.0","username":"巴金"},"msg":"成功!"} 那么返回值是 * DATA的值:{"goodsName":"商品添加","username":"巴金"} */ function sendAjaxRequest(callUrl,dataType) { var resultData = ""; $.ajax( { type : callType,//请求方式 url : callUrl,//地址,就是action请求路径 async : false,dataType : dataType,//数据类型text xml json script jsonp success : function (data) { //返回的参数就是 action里面所有的有get和set方法的参数 resultData = data; } }); return resultData; } //查询我的购物车 @GET @Path("queryMyShopCar") @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") public JSONObject queryMyShopCar(@QueryParam("params") String params) { JSONObject jsonObj = (JSONObject) JSONObject.parse(params); String userid = jsonObj.getString("userid"); ApplicationModule am = Configuration.createRootApplicationModule(Constant.AMDEF,Constant.CONFIG); ViewObject vo = am.findViewObject("QueryProductDetailView1"); QueryProductDetailViewImpl tpo = (QueryProductDetailViewImpl) vo; Row row = tpo.queryMyShop(userid); JSONObject contentObj = null; JSONArray contentArray = new JSONArray(); JSONObject obj = null; try { contentObj = new JSONObject(); contentObj.put(Constant.SUCCESS,true); contentObj.put(Constant.MSG,"查询成功"); contentObj.put(Constant.DATA,contentArray); while (row != null) { obj = new JSONObject(); obj.put("productName",row.getAttribute("Productname") == null ? "" : row.getAttribute("Productname")); obj.put("Addtime",row.getAttribute("Addtime") == null ? "" : row.getAttribute("Addtime")); obj.put("Productprice",row.getAttribute("Productprice") == null ? "" : row.getAttribute("Productprice")); obj.put("Productenname",row.getAttribute("Productenname") == null ? "" : row.getAttribute("Productenname")); obj.put("Shopcarid",row.getAttribute("Shopcarid") == null ? "" : row.getAttribute("Shopcarid")); obj.put("productId",row.getAttribute("Id") == null ? "" : row.getAttribute("Id")); obj.put("Imgurl",row.getAttribute("Imgurl") == null ? "" : row.getAttribute("Imgurl")); contentArray.add(obj); row = tpo.next(); } } catch (Exception e) { e.printStackTrace(); contentObj = new JSONObject(); contentObj.put(Constant.SUCCESS,false); contentObj.put(Constant.MSG,e.getMessage()); contentObj.put(Constant.DATA,contentArray); }finally { Configuration.releaseRootApplicationModule(am,true); } return contentObj; } //删除选中产品 @GET @Path("deleteMyShopCar") @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") public Boolean deleteMyShopCar(@QueryParam("params") String params) { Boolean result = false; JSONObject jsonObj = (JSONObject) JSONObject.parse(params); String shopcarid = jsonObj.getString("shopcarid"); ApplicationModule am = Configuration.createRootApplicationModule(Constant.AMDEF,Constant.CONFIG); try { ViewObject vo = am.findViewObject("TShoppingCartView1"); TShoppingCartViewImpl tpo = (TShoppingCartViewImpl) vo; Boolean bl = tpo.deleteMyShop(shopcarid); if (bl) { am.getTransaction().commit(); result = true; } else { result = false; } } catch (Exception e) { result = false; e.printStackTrace(); } finally { Configuration.releaseRootApplicationModule(am,true); } return result; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |