Home  >  Article  >  Web Front-end  >  jquery serialized form and callback function usage examples_jquery

jquery serialized form and callback function usage examples_jquery

WBOY
WBOYOriginal
2016-05-16 16:42:451264browse

In development projects, the front-end values ​​are passed to the back-end. Sometimes there are one or two values ​​in the JSP form, or all the values. If you pass them one by one at this time, it is definitely not a good idea, so use jQuery to provide The form serialization method can solve this problem very well. At the same time, it can be encapsulated into a general function. If the execution is successful, the respective callback function can be called to realize the respective functions.

The code is as follows:

function queryUserInfo(actionUrl,formId,fun){ 
var params=new Object(); //声明数组 
$.each($("#"+formId).serializeArray(),function(index,param){ 
params[param.name] = param.value; //序列化表单 
}); 
params['time']=new Date(); //1 
$.ajax( { 
url : basePath+actionUrl, 
data : params,//没有1,可以这样写("#"+formId).serializeArray() 
type : 'POST', 
dataType:'json', 
async: false,//表示同步,等待服务端返回数据,才会执行后面的代码 
success : function(obj) { 
fun(actionUrl,formId,obj); 
}, 
error: function() { 
alert("访问异常"); 
} 
}); 
}

Another way:

function setUserInfo(actionUrl,userid,username,fun){ 
var params=new Object(); //声明数组 
params['user.id']=userid; 
params['user.name']=username; 
$.ajax( { 
url : actionUrl, 
data : params,//没有1,可以这样写("#"+formId).serializeArray() 
type : 'POST', 
dataType:'json', 
async: false,//表示同步,等待服务端返回数据,才会执行后面的代码 
success : function(obj) { 
fun(actionUrl,formId,obj);//调用回调的函数 
}, 
error: function() { 
alert("访问异常"); 
} 
}); 
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn