繁体   English   中英

从Ajax传递时,数组值变为null

[英]Array value becomes null while passing from Ajax

我正在我的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(数组)。

在此输入图像描述

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

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

这里我得到数组的空值。

如果我在servlet的简单测试的提交函数中将globalSelection如下所示,我可以得到数组。

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”参数只有“[]”符号

我看到你的问题,我有一个简单的解决方案。

在这种情况下,我建议您将数组转换为JS中的字符串:

JSON.stringify(globalSelection) 

然后使用某种用于JSON转换的库重建后端上的对象,如: https//code.google.com/archive/p/json-simple/

然后你可以做这样的事情:

JSONArray globalSelection = (JSONArray) new JSONParser().parse(request.getParameter("globalSelection"));
    Iterator i = globalSelection.iterator();

    while (i.hasNext()) {
        JSONObject selection = (JSONObject) i.next();
        String type = (String)selection.get("type");
        System.out.println(type);
    }

这将解析您的数组并打印选择类型。 尝试一下,希望它有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM