ajax的应用

     页面中经常会遇见需要更新某个输入框,但是又不至于需要刷新整个页面的情况。这时候就需要使用ajax来做局部页面的更新。这二天刚好写了一个这个功能,简单记录方便以后copy。

    通过省份、运费模板以及重量的变化来计算运费的功能。

    js的脚本如下:

	 jQuery("#showBtn").click(function(){
	 	if(jQuery("#state").valid() && jQuery("#weight").valid()){
			$.ajax({
				url:"${orderPath}${rc.contextPath}/express/freight.html",
				data:{templateId:jQuery("input[name=expessCompany]:checked").val(),weight:jQuery("#weight").val(),stateid:jQuery("#state").val()},
				dataType:"json",
				success:function(json){
					if(json.result){
						jQuery("#preview").html(json.message).show();
					}
				}
			})
		}
	 })
  })

    action脚本,返回json字符串:

	@ResponseBody
	@RequestMapping(value = "/freight")
	public void getExpressPrice(String  templateId, String weight,
			String stateid,
			HttpServletRequest request, PrintWriter out) throws Exception {
		Long itemweight =0L;
		try{
			itemweight = Math.round(Double.valueOf(weight)*1000);
		}catch(Exception e){
			itemweight=0L;
		}
		Long templateIdInt =0l;
		if("shentong".equals(templateId)){
			templateIdInt = 65500l;
		}else{
			templateIdInt = 65501l;
		}
		stateid = stateid(stateid);
		Long freight = expressAddressService.getExpressFreight(templateIdInt, itemweight, stateid);
		JSONObject json=new JSONObject();
		json.put("result", true);
		json.put("message", (double)(Math.round(freight))/100.0+"元");
		out.print(json);
		out.close();

	}

猜你喜欢

转载自5keit.iteye.com/blog/2331593
今日推荐