1

With several checkboxes checked I want to send array when I use $.ajax() but there is null pointer Exception with no parameter in controller.java I attached ajax part of javascript in jsp and part of controller.java

//view part

 var empno = $('input:checkbox[name="checkbox"]:checked');
       var i;
       var array = new Array();

       for(i=0; i<empno.length; i++){

            empnoyo = empno[i].getAttribute("id");//id='{EmpVO.EMPNO}';
            array.push(empnoyo[i]);

       } 

         alert('empnnoyo:'+empnoyo);
         $.ajaxSettings.traditional = true;

       $.ajax({
          url:"/delete",
          data: {array:array},
          dataType: 'text',
          processData: false,
          contentType: false,
          type: 'POST',
          success: function(result){
            if(result==1)  
              alert('delete complete');
              location.href='/index';

          }
      });
  }

//controller part
 @RequestMapping(value="/delete")
public String delete(int[] empno) throws Exception{

    int i=0;
    System.out.println("delete arr:"+empno[i]);
    for(i=0; i<empno.length; i++) {
       service.remove(empno[i]);    
    }   
  return "redirect:index";
}
nowhere
  • 13
  • 3
  • see https://stackoverflow.com/questions/13241668/how-to-send-request-parameter-array-to-servlet-using-jquery-ajax/13241761 – Scary Wombat Feb 16 '18 at 04:05

1 Answers1

1

Remove processData and contentType and change {array:array} to {empno:array}

The processData and contentType thing is only really useful for FormData and Binary types.

Your server-side code expects the parameter as empno not array

   $.ajax({
      url:"/delete",
      data: {empno:array},
      dataType: 'text',
      type: 'POST',
      success: function(result){
        if(result==1)  
          alert('delete complete');
          location.href='/index';

      }
  });
Musa
  • 96,336
  • 17
  • 118
  • 137