前端ajax与后端Spring MVC控制器有以下五种数据交互方式。(前台使用了dhtmlxGrid,后端使用了fastjson)
方式一 通过URL传参
通过URL挂接参数,如/auth/getUser"color: #ff0000">方式二 单值传参
前台调用如:
ajaxPost("/base/user/exchangeSort",{"id":rid,"otherid":otherid},function(data,status){ xxxxxx xxxxxx });
服务器端为:
public String exchangeSort(String id, String otherid)
方式三 对象传参
前台调用如:
var org={id:id}; ajaxPost("/base/org/getOrgById", org,function(data,textStatus){ xxxx xxxx });
服务器端为:
public Org getOrgById(Org org)
方式四 对象序列化传参
前台调用如:
var ueser={id:rowId}; var data=ajaxPost("/base/user/findById",{"userObj":JSON.stringify(user)},null);
或者
var ueser={ };//创建对象 user["id"]=id; user["name"]=$("#name").val(); user["dept"]={};//外键对象 user["dept"]["id"]=$("#deptid").val(); ajaxPost("/base/user/addUser",{"userObj":JSON.stringify(user)},function(data){xxxx;xxxxx;});
服务器端为:
@RequestMapping("/findById") @ResponseBody public UserInfo findById(String userObj) { //使用fastJSON UserInfo user = JSON.parseObject(userObj, UserInfo.class); user = (UserInfo) userService.findById(UserInfo.class, user.getId()); return user; }
方式五 列表传参
前台代码如:
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(); //不推荐这样的写法 //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()!="") { 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"; 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 });
服务器端:
@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"; }
附上ajaxPost代码:
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("调用方法发生异常:"+JSON.stringify(err)+"err1"+ JSON.stringify(err1)+"err2:"+JSON.stringify(err2)); } }); return retData; }
以上这篇Spring MVC前端与后端5种ajax交互方法【总结】就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略
- 《七龙珠电光炸裂!ZERO》GT角色预告片曝光,15位新角色登场
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-7
- 雨果唱片-三角琴《俄罗斯民间音乐系列-梁祝(中国名曲)》wav
- 《塞尔达传说:智慧的再现》新实机!无之世界介绍
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 任天堂更新网络投稿规范:打击影响他人游戏体验行为
- 冼佩瑾.2024-女儿Daughter(EP)【杰威尔】【FLAC分轨】
- 黑豹乐队.1993-光芒之神【摇音晨【WAV+CUE】
- 曾轶可.2011-一只猫的旅行【天娱传媒】【WAV+CUE】
- 黑神话悟空上品狼刺客精魄获取方法一览|上品狼刺客精魄收集攻略
- 视觉小说《神椿市建设中REGENERATE》将于2025年2月20日发售