我正在我的 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)。
在我的 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/