Springmvc back-end parameters, arrays, collections, complex objects, etc.
Reference Address: 54705933
Several common ways are as follows: (1) Arrays: Backstage
@ResponseBody @RequestMapping (value= "/ajaxsortpriority") PublicResultdo ajaxsortpriority (@RequestParam ("ids[]") long[] (IDs) {Resultdo Resultdo=NewResultdo (); intSize=cmsbannerservice.sortpriority (IDS); if(size==ids.length) {resultdo.setsuccess (true); }Else{resultdo.setsuccess (false); } returnResultdo; }
Front
varparam=[]; $("#tb_order"). Find ("Td[name= ' id ']"). each (function() {Param.push ($ ( This). text ()); }) varids={Ids:param}; $.ajax ({cache:true, type:"GET", URL:"/cmsbanner/ajaxsortpriority", DataType:"JSON", Data:ids, async:false, Success:function(data) {
(2) Collection
Background
@RequestMapping (value = "/cfgrepayremind", method =requestmethod.post) @ResponseBody PublicResultdo<?>cfgrepayremind (@RequestBody List<SysDictPojo> Sysdictpojos//the data format requested by the @RequestBody foreground must be JSON) {Resultdo<?> Resultdo =Resultdo.build (); Try{icprojectrepayservice.cfgrepayremind (Sysdictpojos); } Catch(Exception e) {resultdo.setsuccess (false); } returnResultdo; }
Pojo class
public classextends abstractbasepojo { private Long id; Private String key; Private String value; Private String description; }
Front
function Cfgrepayremind (ele) {var URL= $ (ele). attr ("value"); var params= []; $("#repayRemindMobile"). Find ("UL")). each (function () {var id= $( This). Find ("Input[name= ' id ']"). EQ (0). Val (); var value= $( This). Find ("input[name= ' value ']"). EQ (0). Val (); Params.push ({id:id, value:value});//Id,value is the attribute in the Java bean, the name is identical}) $.ajax ({cache:true, type:"POST", Url:url, data:JSON.stringify (params),//specifies that the requested data format is JSON, which actually passes the JSON stringContentType: ' Application/json;charset=utf-8 ',//specifies that the requested data format is JSON so that the background can accept Java beans with @requestbodyDataType: "JSON", Async:false, Success:function (data) {if(data.success) {toastr.success ("Operation succeeded"); SetTimeout (function () {location.reload (); }, 1000) } } }); }
(3) Single parameter
Front
$.ajax ({ type:"POST", data:{total:' + '}, DataType:' json ', URL: "Http://127.0.0.1:8089/icProject/test", success:function () { } } )
Background
@RequestMapping (value = "/test", method = requestmethod.post) @ResponseBody public String Test (@RequestParam ("Total"), String total ) {returnnull; }
(3) Pass multiple parameters
Front
var tagids = []$.ajax ({ "POST", "/auth/childcomment/createcomment", Data: { ID: $("#sourceId"). Val (), CourseID: $ ("#courseId"). Val (), classId: $ ( "#commentClassId"). Val (), content: $ ("#updateContent"). Val (), childID: $ (" #childId "). Val (), Classcatalogid: $ (" #classCatalogId "). Val (), tagid:tagids },
Background
@PostMapping ("Createcomment") @PreAuthorize ("hasanyauthority (' Merchant:childComment:index ')") @ResponseBody Public resultdo createcomment ( = "childID") long childid, = "CourseID") long CourseID, /c10>= "ClassId") long classId, = "Classscheduleid") long Classscheduleid, = "Content ") String content, false) list<long> tagids ) {
(4) Form data serialization parameter, Ajax commit
Front
var params = $ ("#sysUserFrm"). Serialize (); var url = "/sysuser/settingsave" $.ajax ({ true, "POST", Url:url, data:params, "JSON", false, function (data) {} })
Background
@RequestMapping (value = "/settingsave", method = requestmethod.post) @ResponseBody public Resultdo<?> Settingsave (Sysuserpojo sysuserpojo) {}
This sysuserpojo can also receive the bean, in fact, the $ ("#sysUserFrm"). Serialize () is equivalent to assembling the JSON object {}
You must first assemble the JSON object {username: "Carter"}
My habit is to use the @RequestBody to accept the object with the List object or List object, because JS has a push method to combine to get the list is relatively simple
Do not use contenttype: "Application/json" then data can be an object, using contenttype: "Application/json" then data can only be a JSON string
Use @requestbody (actually accept a JSON string)
function logined () { $.ajax ({ "POST", "/backend/logined", data:JSON.stringify ({ UserName:"Cater", Password: "123456"}),// turn to String contentType: "Application/json;charset =utf-8 ",// do not use contenttype:" Application/json "then data can be an object, using contenttype:" Application/json " Then data can only be JSON string function (data) {} }) }
Springmvc back-end parameters, arrays, collections, complex objects, etc.