전단 ajax 와 백 엔 드 Spring MVC 컨트롤 러 데이터 상호작용 방식

전단 ajax 와 백 엔 드 Spring MVC 컨트롤 러 는 다음 과 같은 다섯 가지 데이터 교환 방식 이 있 습 니 다.(프론트 데스크 에 dhtmlxGrid 를 사 용 했 고 백 엔 드 에 fastjson 을 사 용 했 습 니 다)
예 를 들 어 / auth / getUser? userid = '6' 서버 엔 드 방법 은 getUser (String userid) 로 작성 할 수 있 으 며, HttpSession, HttpServletRequest, HttpServletResponse, Mode, ModelAndView 등 다른 매개 변 수 를 추가 할 수 있 습 니 다.
방식 2 단 값 전송 프론트 데스크 톱 호출: ajax Post ("/ base / user / exchangeSert", {"id": rid, "otherid": otherid}, function (data, status) {xxxxx xxxxx});
서버 쪽: public String exchangeSert (String id, String otherid)
방식 3 개체 전송 프론트 데스크 톱 호출: var org = {id: id};ajaxPost("/base/org/getOrgById", org,function(data,textStatus){ xxxx xxxx }); 서버 쪽: Public Org getOrgById (Org org)
방식 4 개체 직렬 화 전 참 프론트 데스크 톱 호출: 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;}
방식 5 목록 참조 프론트 코드: var objectList = new Array ();grid. forEchrow (function (rId) {var index = grid. getRowIndex (rId); var obj = {}; obj ["id"] = rId; obj ["user"] = {}}; obj ["user"] ["id"] = $("\ # userId"). val (); / / / 이러한 쓰 기 를 추천 하지 않 습 / / / / / / / "kinShip"] = grid. ["kinShip"] = grid. cell (rId, 1). getVal값 (); / / / / obj [name "] = grid. cell. cells (rId 값 값, 2). get값 값 값 값 값 (2). getI............. / / /); 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"; }
    ajax Post 코드 첨부:
    
    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;
    }
    ···

    좋은 웹페이지 즐겨찾기