ajax json list spring mvc接收

前端之家收集整理的这篇文章主要介绍了ajax json list spring mvc接收前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

后台接收

/** 保存新增,@Valid标注spirng在绑定对象时自动为我们验证对象属性并存放errors在BindingResult  */
	@RequestMapping(method=RequestMethod.POST)
    @ResponseBody
	public  Map<String,Object> create( @RequestBody  List<UserSetting> data,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		int count = -2 ;
        if(data!=null&&RequestUtils.getCurrentUser()!=null)
            count = omUserNoticeSettingManager.save(data,RequestUtils.getCurrentUser().getUserId());
		//Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("result","success");
        result.put("count",count);
        return result;
	}

前台

noticeSetting.sumbitform = function(btn){
    var itemArray = new Array();
    var  ispass = true;
    var errorIndex;
    $.each($("#settingtable tbody tr"),function(i,item){
        var selects = $(this).find("select");
        var deviceTypeS = $(selects.get(0)).data("kendoDropDownList");
        var deviceStatuS =$(selects.get(1)).data("kendoDropDownList");
        var branchareas = $(selects.get(2)).data("kendoMultiSelect");
        var item ={"deviceTypeId":parseInt(deviceTypeS.value()),"deviceStatus":deviceStatuS==null?null:parseInt(deviceStatuS.value()),"branchareas":branchareas==null?null:branchareas.value()};
        itemArray.push(item);
        if(deviceStatuS ==null||branchareas==null||branchareas.value().length==0){
            errorIndex=i+1;
            ispass = false;
            return false;
        }
    });
    if(!ispass){
        btn.removeAttr("disabled");
        $("#addtrbtn").removeAttr("disabled");
        $("#sumbitbutton").data("result","");
        noticeSetting.notify("第"+errorIndex+"行没选择完整","error");
        return false;
    }
    $.ajax({
        url: baseContext+"/omusernoticesetting.json",cache: false,dataType:"json",contentType:"application/json",type:"post",data:JSON.stringify(itemArray),success: function(data){
            $("#sumbitbutton").data("result","success");
            $("#sumbitbutton").data("count",data.count);
            var count = parseInt(data.count);
            if(count==0){
                noticeSetting.notify("重复设置不会添加","error");
            }else if(count>0){
                noticeSetting.notify("成功添加"+count+"条设置","success");
            }else{
                noticeSetting.notify("添加失败","error");
            }
            btn.removeAttr("disabled");
            $("#addtrbtn").removeAttr("disabled");
        },error:function(data){
            btn.removeAttr("disabled");
            $("#addtrbtn").removeAttr("disabled");
            $("#sumbitbutton").data("result","error");
            noticeSetting.notify("添加失败","error");
        }
    })

}
原文链接:https://www.f2er.com/ajax/163596.html

猜你在找的Ajax相关文章