$("#btnDel").on("click",function(){
var idsarray = [];
$("input:checkbox[name=delbox]:checked").each(function() {
idsarray.push($(this).val());
});
console.log(idsarray);
var idsString = idsarray.join();
console.log(idsString);
$.ajax({
url: 'StudentController',
dataType: 'json',
data: {
"idsToBeDeleted" : idsString
},
type: 'DELETE'
});
});
这是JS代码,我捕捉复选框的id,并将它们作为逗号分隔的字符串发送给servlet。问题是,当我试图在servlet上捕获变量并将其打印出来时,我得到的全部是空。
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//catching values from recieved ajax request
String idlistString = (String) request.getParameter("idsToBeDeleted");
System.out.println(idlistString);
response.sendRedirect(request.getContextPath());
}
servlet代码在这里,一些关于如何通常捕获通过ajax请求发送的数据的解释将是感激的。
尝试在url前加一个斜杠并键入:'POST'。还要注意我所做的小改动。
$.ajax({
url: '/StudentController',
type: 'POST',
dataType: 'json',
data: {
operation: 'remove',
idsToBeDeleted: idsString
},
type: 'DELETE'
});