Examples of using jquery serialized forms and callback functions
- 2020-03-30 03:30:57
- OfStack
In development projects, to pass the value of the foreground to the background, sometimes one or two JSP in the form of values, there are total value, if this one by one, surely not a good way, so use the jQuery form serialization method, can well solve this problem, at the same time can be encapsulated into the general function, implementation success can call the respective callback function, realize their respective functions.
The code is as follows:
function queryUserInfo(actionUrl,formId,fun){
var params=new Object(); //Declare an array
$.each($("#"+formId).serializeArray(),function(index,param){
params[param.name] = param.value; //Serialized form
});
params['time']=new Date(); //1
$.ajax( {
url : basePath+actionUrl,
data : params,//There is no 1, you can write ("#"+formId).serializearray ()
type : 'POST',
dataType:'json',
async: false,//Represents synchronization, waiting for the server to return data before the following code is executed
success : function(obj) {
fun(actionUrl,formId,obj);
},
error: function() {
alert(" Access exception ");
}
});
}
Another way:
function setUserInfo(actionUrl,userid,username,fun){
var params=new Object(); //Declare an array
params['user.id']=userid;
params['user.name']=username;
$.ajax( {
url : actionUrl,
data : params,//There is no 1, you can write ("#"+formId).serializearray ()
type : 'POST',
dataType:'json',
async: false,//Represents synchronization, waiting for the server to return data before the following code is executed
success : function(obj) {
fun(actionUrl,formId,obj);//Calls the callback function
},
error: function() {
alert(" Access exception ");
}
});
}