Five ajax interaction methods between Spring MVC frontend and backend
Frontend ajax and backend Spring MVC controllers have the following five Data Interaction methods. (DhtmlxGrid is used at the front end and fastjson is used at the backend)
Method 1: PASS Parameters through URL
Mount parameters through URL, such as/auth/getUser? Userid = '6'
The server method can be written as getUser (String userid). Other parameters such as HttpSession, HttpServletRequest, HttpServletResponse, Mode, and ModelAndView can also be added.
Method 2 single-value parameter passing
Frontend calls include:
AjaxPost ("/base/user/exchangeSort", {"id": rid, "otherid": otherid },Function(Data, status ){
Xxxxxx
Xxxxxx
});
The server side is:
Public String exchangeSort (String id, String otherid)
Method 3: Passing parameters from an object
Frontend calls include:
Var org = {id: id };
AjaxPost ("/base/org/getOrgById", org, function (data, textStatus ){
Xxxx
Xxxx
});
The server side is:
Public Org getOrgById (Org org)
Method 4 Object serialization parameter passing
Frontend calls include:
Var ueser = {id: rowId };
Var data = ajaxPost ("/base/user/findById", {"userObj": JSON. stringify (user)}, null );
Or
Var ueser ={}; // create an object
User ["id"] = id;
User ["name"] = $ ("# name"). val ();
User ["dept"] ={}; // Foreign key object
User ["dept"] ["id"] = $ ("# deptid"). val ();
AjaxPost ("/base/user/addUser", {"userObj": JSON. stringify (user)}, function (data) {xxxx; xxxxx ;});
The server side is:
@ RequestMapping ("/findById ")
@ ResponseBody
PublicUserInfo findById (String userObj ){
// Use fastJSON
UserInfo user = JSON. parseObject (userObj, UserInfo.Class);
User = (UserInfo) userService. findById (UserInfo.Class, User. getId ());
ReturnUser;
}
Method 5: Passing parameters in the list
The front-end code is as follows:
VarObjList =NewArray ();
Grid. forEachRow (Function(RId ){
VarIndex = grid. getRowIndex (rId );
VarObj = {};
Obj ["id"] = rId;
Obj ["user"] = {};
Obj ["user"] ["id"] = $ ("# userId"). val ();
// This method is not recommended
// Obj ["kinShip"] = grid. cells (rId, 1). getValue ();
// Obj ["name"] = grid. cells (rId, 2). getValue ();
Obj ["kinShip"] = grid. cells (rId, grid. getColIndexById ("columnName"). getValue ();
Obj ["name"] = grid. cells (rId, grid. getColIndexById ("name"). getValue ();
If(Grid. cells (rId, 3). getValue ()! =Null& Grid. cells (rId, 3). getValue ()! = ""){
VarStr = grid. cells (rId, 3). getValue (). split ("-");
VarDay = parseFloat (str [2]);
VarMonth = parseFloat (str [1])-1;
VarYear = parseInt (str [0]);
VarDate =NewDate ();
Date. setFullYear (year, month, day );
Obj ["birth"] = date;
}Else{
Obj ["birth"] = "";
}
Obj ["politicalStatus"] = grid. cells (rId, 4). getValue ();
Obj ["workUnit"] = grid. cells (rId, 5). getValue ();
If(Grid. cells (rId, 6). isChecked ())
Obj ["isContact"] = "1 ";
Else
Obj ["isContact"] = "0 ";
Obj ["phone"] = grid. cells (rId, 7). getValue ();
Obj ["remark"] = grid. cells (rId, 8). getValue ();
Obj ["sort"] = index;
ObjList. push (obj );
});
AjaxPost ("/base/user/addUpdateUserHomeList ",{
"UserHomeList": JSON. stringify (objList ),
"UserId": $ ("# userId"). val ()
},Function(Data, status ){
Xxxxx
});
Server:
@ RequestMapping ("/addUpdateUserHomeList ")
@ ResponseBody
Public String addUpdateUserHomeList (String userHomeList, String userId ){
List UserHomes = JSON
. ParseArray (userHomeList, UserHome. class );// FastJSON
If (userHomes! = Null & userHomes. size ()> 0 ){
Try {
UserService. addUpdateUserHomeList (userHomes, userId );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Return "200 ";
}
Add the ajaxPost code:
Function ajaxPost (url, dataParam, callback) {var retData = null; $. ajax ({type: "post", url: url, data: dataParam, dataType: "json", success: function (data, status) {// alert (data ); retData = data; if (callback! = Null & callback! = "" & Callback! = Undefined) callback (data, status) ;}, error: function (err, err1, err2) {alertMsg. error ("Call method exception:" + JSON. stringify (err) + "err1" + JSON. stringify (err1) + "err2:" + JSON. stringify (err2) ;}}); return retData ;}