JSP/javascript jQuery

[jQuery] AJAX 사용 방법

도미노& 2019. 10. 28. 16:44

1-1. 클라이언트

var request = $.ajax({

url: "aa",

method: "POST",

data: {

aaJsonData : JSON.stringify(obj)

} ,

dataType: "json"

});

request.done(function( msg ) {

if(msg.result == "false"){

console.log("exception 처리");

} else {

if( status == 'temp' ){

alert('임시저장이 완료되었습니다.');

} else {

alert('등록되었습니다.');

location.href="/import/list.ksif";

}

}

});  

request.fail(function( jqXHR, textStatus ) {

// alert( "Request failed: " + textStatus );

alert('저장에 실패하였습니다.');

});

request.always(function() {

// 항상 실행

});



1-2. 서버 컨트롤러

  @RequestMapping(value = "aa", method = RequestMethod.POST)

public ModelAndView mergeImportData(HttpServletRequest req, HttpServletResponse res

, @RequestParam Map<String, Object> paramMap) throws Exception {

Map<String, Object> resultMap = new HashMap<String, Object>();

JSONObject jsonObject = new JSONObject();

try {

service.mergeData(req, paramMap);

jsonObject.put("ajaxDocId", importService.selectTbDocImportKey(paramMap)); // 필요한 건 jsonObject에 put해서 클라이언트단에서 꺼내야 함

resultMap.put("result", jsonObject);

} catch (Exception e) {

ExceptionUtils.getStackTrace(e);

throw e;

}

return new ModelAndView("jsonView", resultMap);

}



1-3. 서버 서비스

    public String mergeImportData(HttpServletRequest req, Map<String, Object> paramMap) throws Exception {

Authentication authentication = AuthenticationHelper.getAuthentication(req);

    String userId = authentication.getRole().getUserId();

String hakdangId = authentication.getRole().getHakdangId();

    String paramJsonData = (String) paramMap.get("importJsonData");

   

    // json to map

    ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> dataMap = mapper.readValue(paramJsonData, new TypeReference<Map<String, Object>>(){});


   

        return "";

    }




2-1. 클라이언트


      $.ajax({

        url: 'url'

        , type: "POST"

        , data: {'params':prams}

        , async: false

        , success: function(data){

          // alert("실패한 번호: " + data);

          failphoneno += data + ", ";

        }

      });


2-2. 서버


String strparam = UtilBean.decodeParmUrl(request.getParameter("smsparams"));