function getFormJson(form) {
var o = {};
var a = $(form).serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
}
在ajax请求后台处理过程当中,ajax请求方法书为:
var MajorTestresult;
var json=getFormJson($("#majorTestForm"));//调用该方法将表单序列化为JSON字符串
var jsonstr=JSON.stringify(json,null,"\t");
if($("input:checked").length==ExamCount){
$.ajax({
url:'/qualtitytest/submitZqmajorTestData',
type:"post",
contentType:"application/json",//设置contentType:"application/json"
dataType:'json',
data:jsonstr,
beforeSend:function () {
MajorTestresult=new simpleAlert({"content": "正在提交测试结果,请稍等。。。",})//ajax请求处理前调用刷新操作
},
success:function(data) {
MajorTestresult.close();
if(data.success){
new TipBox({type:'success',str:'数据处理成功,5s后自动跳转',setTime:5000,callBack:function(){
window.location.href="/qualtitytest/zqcp_index";
}});
}else{
new TipBox({type:'error',str:data.msg,hasBtn:true});
}
},
error:function () {
alert("出错了,请联系管理员!");
}
})
在后台使用@RequestBody String jsonstr,可以将json字符串装换成json对象,在使用json对象的处理方法取值进行操作即可,最重要的操作就是数据组装。