1. 程式人生 > >ajax 傳遞陣列到後臺

ajax 傳遞陣列到後臺

背景:
昨天遇到一個坑,好幾個小時都沒有解決,就是ajax往後臺傳遞陣列的時候。後臺取到的總是為空。後來知道是spring mvc 強制序列化了。要在ajax 程式碼中加一個屬性用來防止序列化
ajax程式碼

 $.ajax({
        "url": "testplan/runPlanByPlanId",
        "type": 'POST',
        "data" :{'planId': myArray},
        traditional: true,
        dataType:'json',
        success: function(data)
{
} });

後臺程式碼:

//planId
    @RequestMapping("/runPlanByPlanId")
    @ResponseBody
    public ResultVO runPlanByPlanId(Long[] planId){
    logger.info("TestPlanController.deleteTestPlan started!");
    List<Long> list = Arrays.asList(planId);

    ResultVO resultDto = new ResultVO();
resultDto.setCode(Constants.SUCCESS_STATUS_CODE); resultDto.setMessage(Constants.DELETE_SUCCESS_MSG); try{ if(list!=null){ String message = testPlanService.runTestPlanByPlanId(list); resultDto.setMessage(message); }else{ resultDto.setMessage("no checked"
); } }catch(Exception e){ logger.error(e.getMessage()); resultDto.setCode(Constants.FAILED_STATUS_CODE); resultDto.setMessage(Constants.DELETE_FAILED_MSG); } logger.info("TestPlanController.deleteTestPlan end!"); return resultDto; }