【问题标题】:i'm not able to catch data send through ajax request to servlet [duplicate]我无法捕获通过 ajax 请求发送到 servlet 的数据 [重复]
【发布时间】:2021-11-11 05:48:09
【问题描述】:
$("#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 请求捕获发送的数据的解释将不胜感激。

【问题讨论】:

    标签: javascript java jquery ajax servlets


    【解决方案1】:

    尝试在您的网址前加上斜杠并输入:“POST”。还要注意我所做的小改动。

    $.ajax({
        url: '/StudentController',
        type: 'POST',
        dataType: 'json',
        data: {
            operation: 'remove',
            idsToBeDeleted: idsString
        },
        type: 'DELETE'
    });
    

    【讨论】:

    • 我已经将请求发送到服务器,问题只是我无法捕获我正在发送的数据的值(我也是要删除的请求类型,可以'无论如何都不要改变)。
    猜你喜欢
    • 2019-04-18
    • 2014-09-15
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2013-10-22
    相关资源
    最近更新 更多