Issue
function getboxvalues(){
var array = [];
$("input:checkbox[name=delbox]:checked").each(function() {
array.push($(this).val());
});
JSON.stringify(array);
console.log(array);
$.ajax({
url: 'Deletestudent',
dataType: 'json',
data: {
object: {"delbox":array}
},
type: 'GET'
});
}
Edit : this is the servlet Code i might have to parse the object im getting but as i was trying to get it to work , most people were just suggesting accessing it through the ususal way .
public class Deletestudent extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Deletestudent() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] idlistString = request.getParameterValues("delbox");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("delbox"));
doGet(request, response);
}
}
i tried accessing it through request.getparameter , but it returns as null eventho i tried logging it in the JS file and it comes out as a normal array.
Solution
You have two problems:
- You have forgotten about
object
entirely even though you explicitly put it in your data - jQuery follows the PHP-ism of renaming parameters with array values so they have
[]
appended to them and you didn't account for that.
Your URL is going to look something like:
/Deletestudent?object%5Bdelbox%5D%5B%5D=foo&object%5Bdelbox%5D%5B%5D=bar
So you need to ask something with the right name.
request.getParameter("object[delbox][]"));
Answered By - Quentin
Answer Checked By - Robin (JavaFixing Admin)