日期:2014-05-16  浏览次数:20322 次

java异步验证且json字符串传递

  有时候在项目中需要异步验证,并通过json和字符串传递数据。

第一种是对象转为json:

var params={
                "recordId":TLD.record_id,
                "item_id":itemid.val(),
                "tj_value":$(this).val(),
                "is_many_range":$(this).attr("is_many_range")
            };
$.ajax({
	  			type:"POST",
	  			url : "/deptex/scopedis.htm", 
	  			dataType : "JSON",
	  			data:params,//你需要的传入后台的参数
	  			async:false,//必须同步,否则开关bvalue无效
	  			global:false,
	  			success : function(data){
	  				//is_scope 是bean的属性
	  				if(data.is_scope=="1"){
	  					bvalue = false;
	  				}
	  				if(data.is_yang==TLD.IF_Y){
	  					isyang.attr("checked",true);
	  					if(tipsValue){
	  						tips_content.val(data.tips_content);
	  					}
});


在action层的写法:

    Disease disease = deptEnterService.findItemScopeRelDisease(recordId, itemId, tjValue, is_many_range);//从数据库中查询出来的数据

        JSONObject json = JSONObject.fromObject(disease); //转化为json对象
       
            response.setCharacterEncoding("GBK");
            response.setContentType("application/json; charset=gbk");
            response.getWriter().write(json.toString(1, 1));
            return Action.NONE;
====================

第二种是字符串传递参数并且字符串返回。

页面:

$.ajax({
  			type:"POST",
  			url : "/deptex/ajaxRecordByIdIsPay.htm",
  			dataType : "text",
  			data:"recordId="+recordId,
  			success : function(data){
                                alert(data);
                                if(TLD.isTjSysytemPay=="1" && data=="-1")
  				{
  					alert("该体检人员存在未付费项目,不能进去科室录入");
  					return;
  				}
  				else {
  					self.location.href = TLD.domain+"/deptex/toenter.htm?recordId="+recordId+"&tj_type="+tj_type;;
				}
  				
  			}
  		});

action层:

            String  IsPay = "-1";
            response.setCharacterEncoding("GBK");
            response.setContentType("application/text; charset=gbk");
            response.getWriter().write(IsPay);

            return Action.NONE;

=========================

第三种:集合转化为json

页面:

$.ajax({
		type:"POST",
		cache:false,
		url : "/record/pkgitem.htm",
		dataType : "json",
		data:{pkgid:pkg.id},
		async:false,
		success : function(data){//data为集合
                       for(var i=0;i<data.length;i++){
				data[i].package_id=pkg.id;
				data[i].package_name=pkg.package_name;
				data[i].discount=pkg.discount;
				addItemGroup(data[i]);
			}
		},
		beforeSend:function(){
			
		},
		complete: function(){
			
		}
	});
action层:

    List<ItemGroup> itemgrouplist = packageRelItemGroupService.findIte