Jquery 通过Ajax发送Servlet的值

Jquery 通过Ajax发送Servlet的值,jquery,ajax,servlets,map,Jquery,Ajax,Servlets,Map,晚安 就我个人而言,我有一个模拟“映射”的函数,我在其中添加“键”菜单项和“值”菜单子项 此功能可在下面查看 var key; var value; var mapaDeListas = new Array(); $(".conteudoMenu a").click(function(){ key = $(this).parent(".conteudoMenu").parent("li").find(".itemMenu").attr("id"); value = $(this).a

晚安

就我个人而言,我有一个模拟“映射”的函数,我在其中添加“键”菜单项和“值”菜单子项

此功能可在下面查看

var key;
var value;
var mapaDeListas = new Array();
$(".conteudoMenu a").click(function(){
   key = $(this).parent(".conteudoMenu").parent("li").find(".itemMenu").attr("id");
   value = $(this).attr("id"); 
   Processar();
   console.log(mapaDeListas);
});

var Processar = function() {
    if (mapaDeListas[key] && $.inArray(value, mapaDeListas[key]) == -1 || mapaDeListas[key] && $.inArray(value, mapaDeListas[key]) != -1) {
        // Checks if the key already exists from the "key" and if the value of "value" is present in the array
       if (mapaDeListas[key].indexOf(value) == -1){
            mapaDeListas[key].push(value);
        }
    }
    else {
        // Creates the array according to the value of the "key" and adds the value of "value"
        mapaDeListas[key] = new Array();
        mapaDeListas[key].push(value);
    }
}
我想知道的是如何在Ajax中将此函数的值传递给Servlet,并通过它们在java中组装映射

我试图通过Ajax发送此函数的值,如下所示,但没有效果

function loadQuery(){
    if(inputMenu.length > 0){
    $.ajax({
        url: "assembles-query",
        type: "POST",
        data: {
            "mapaDeListas[]" : mapaDeListas
        },
        //dataType: "json",
        error:function(){
            alert("ERRO MENU")
        },
        success:function(responseText){
            $("textarea[id=assembleQuery]").text(responseText);
        }
    });
    }
}
在Servlet中,尝试获得如下结果String[]mapaDeListas=request.getParameterValues(“mapaDeListas[])

但是,当我发送打印时,结果总是返回null

请有人告诉我哪里错了,我应该怎么做才能正确传递这些值? 因为我已经感谢大家的帮助了

下面打印了如何存储结果。

我假设jQuery 1.4就是这样-然后使用
traditional:true
参数

或者全局设置:()

从jQuery1.4开始,
$.param()
方法递归地序列化深层对象,以适应现代脚本语言和框架,如PHP和RubyonRails。通过设置
jQuery.ajaxSettings.traditional=true,可以全局禁用此功能


我不确定这个
“mapaDeListas[]”:mapaDeListas
是否可行。可能您应该尝试构建自己的GET URL(
URL:“assembles query”,
)并附加数组的每个元素。当我尝试在loadQuery函数()中仅打印mapaDeListas时,{}不会返回任何内容。这就好像这些值没有存储在mapaDeListas中一样。这就是你说的建议?你好,苏达卡。因为这可以帮助我解决我的问题?非常感谢。