Five ajax interaction methods between the front-end and backend of Spring MVC [summary], mvcajax

Source: Internet
Author: User
Tags object serialization

Five ajax interaction methods between the front-end and backend of Spring MVC [summary], mvcajax

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){xxxxxxxxxxxx});

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){xxxxxxxx});

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 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") @ ResponseBodypublic UserInfo findById (String userObj) {// use fastJSONUserInfo user = JSON. parseObject (userObj, UserInfo. class); user = (UserInfo) userService. findById (UserInfo. class, user. getId (); return user ;}

Method 5: Passing parameters in the list

The front-end code is as follows:

Var objList = new Array (); grid. forEachRow (function (rId) {var index = grid. getRowIndex (rId); var obj ={}; obj ["id"] = rId; obj ["user"] = {}; obj ["user"] ["id"] = $ ("# userId "). val (); // This statement 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. getColInd ExById ("name"). getValue (); if (grid. cells (rId, 3). getValue ()! = Null & grid. cells (rId, 3). getValue ()! = "") {Var str = grid. cells (rId, 3 ). getValue (). split ("-"); var day = parseFloat (str [2]); var month = parseFloat (str [1])-1; var year = parseInt (str [0]); var date = new Date (); 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"; elseobj ["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")@ResponseBodypublic String addUpdateUserHomeList(String userHomeList, String userId) {List userHomes = JSON.parseArray(userHomeList, UserHome.class);//fastJSONif (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 ;}

The above five ajax interaction methods between the Spring MVC front-end and the backend [summary] are all the content that I have shared with you. I hope you can give me a reference and support me a lot.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.