javascript - 从 Ajax 传递时数组值变为 null

标签 javascript java arrays ajax servlets

我正在我的 javascript 提交函数中进行 ajax 调用。在此 ajax 调用中,我将数组(globalSelection)作为数据传递给 servlet。该数组由函数 textSelection 的元素组成,该函数也粘贴在下面。

globalSelection =[];

function submit() {

    console.log("globalSelection start")
    console.log(globalSelection)
    console.log("globalSelection end")

        $.ajax({
            async : false,
            type : "POST",
            url : 'http://example.com:8080/myApp/DataServlet',
            data: {globalSelection:globalSelection},
            success : function(data) {
                alert(data)
            },
            error : function(data, status, er) {
                alert("error: " + data + " status: " + status + " er:" + er);
            }
        }); 

}

function textSelection(range, anchorNode, focusNode) {
    this.range = range;
    this.type = 3;
    this.rCollection = [];
    this.textContent = encodeURI(range.toString());
    this.anchorNode = anchorNode;
    this.focusNode = focusNode;
    this.selectionId = getRandom();
    this.yPOS = getYPOS();

    this.getTagName = function(range) {
        var el = range.startContainer.parentNode;
        return el;
    }
    this.getTagIndex = function(el) {
        var index = $(el.tagName).index(el);
        return index;
    }

    this.simpleText = function(node, range) {
        if (!node)
            var entry = this.createEntry(this.anchorNode, this.range);
        else
            var entry = this.createEntry(node, range);
        this.rCollection.push(entry);
        this.highlight(this.rCollection[0].range);
        this.crossIndexCalc();
        textSelection._t_list.push(this);
        pushto_G_FactualEntry(this);
    }

    this.compositeText = function() {
        this.findSelectionDirection();
        var flag = this.splitRanges(this.anchorNode, this.focusNode,
                this.range.startOffset, this.range.endOffset);
        if (flag == 0) {
            for (j in this.rCollection) {
                this.highlight(this.rCollection[j].range);
            }
        }
        this.crossIndexCalc();
        textSelection._t_list.push(this);
        pushto_G_FactualEntry(this);
    }

}

我正在下面添加浏览器控制台的屏幕,它打印 globalSelection(array)。

enter image description here

在我的 servlet 中,我得到这个数组如下

String[] arrays = request.getParameterValues("globalSelection[]");
System.out.println(arrays);

这里我得到了数组的空值。

如果我将 globalSelection 放入提交函数中以对 servlet 进行简单测试,我就能够获取数组。

var globalSelection = ["lynk_url", "jsonBody", "lynk_dummy1", "lynk_dummy2", "lynk_name", "lynk_desc", "lynk_flag"];

为什么我的实际 globalSelection 在 servlet 中显示为 null,我在这里做错了什么。

最佳答案

尝试使用: String[] arrays = request.getParameterValues("globalSelection"); System.out.println(数组);

因为仅使用名称“globalSelection”提交的参数,而不使用“[]”符号

关于javascript - 从 Ajax 传递时数组值变为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661455/

相关文章:

java - 添加/更改影响父级 setBounds 的 JLabels

arrays - 如何判断一个数组是否包含一个字符串

javascript - 在 JSON 中添加新元素

java - 运行 Ant 脚本时出现代理问题

java - 为什么在java中将类变量设为私有(private)而将方法设为公共(public)更好

jquery css 使用数组作为值

javascript - 如何在 typescript 中添加扩展泛型类的构造函数?

javascript - 如何覆盖 Kendo UI ListView 的默认键盘导航

javascript - 当第 n 个嵌套数组不再有嵌套数组时停止的递归函数

JavaScript - getElementsByName