簡體   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