如何使用数组和字符串参数进行Ajax调用 [英] how to make Ajax call with array and string parameters
问题描述
我想用数组值和sting参数对我的服务器进行ajax调用。
I want to make ajax call to my server with array value and sting parameters.
这是我在我的页面中使用的函数。
This is my function I am using in my page.
var globalArray = [];
function submit() {
var string_MyVal, jsonBody, string_MyVal1, string_MyVal2, string_MyNameVal, string_MyNameDesc, string_MyNameFlag;
string_MyVal1 = 150;
string_MyVal = "sel.html";
string_MyVal2 = "string_MyVal2";
string_MyNameVal = "string_MyNameVal";
string_MyNameDesc ="string_MyNameDesc";
string_MyNameFlag = "private";
jsonBody = 'storedSelections=' + globalArray +
'&string_MyVal='+ string_MyVal +
'&string_MyVal1='+ string_MyVal1 +
'&string_MyVal2='+ string_MyVal2 +
'&string_MyNameVal=' + string_MyNameVal +
'&string_MyNameDesc='+ string_MyNameDesc +
'&string_MyNameFlag=' + string_MyNameFlag;
$.ajax({
async : false,
type : "POST",
url : 'http://example.com:8080/myApp/DataServlet',
data : jsonBody,
success : function(data) {
setToken(data);
},
error : function(data, status, er) {
alert("error: " + data + " status: " + status + " er:" + er);
}
});
}
在我的servlet中,这个globalArray只是对象对象。该数组中有更多内容..
In my servlet this globalArray comes as only "object object". There is more contents in that array..
如何将此数组和字符串值传递给我的servlet。
how to pass this array and string values to my servlet.
我知道使用JSON.stringify解决了这个问题,
I know to use JSON.stringify solved this,
var selections = JSON.stringify(globalSelection);
alert(selections
这有效,数据显示如下,
This works and the data is shown as below,
[{"range":{},"type":3,"rCollection":[{"range":{},"node":{},"tagName":"P","tagIndex":2,"data":"lot%20","nodeType":3,"sIdx":14,"eIdx":18,"fontColor":"yellow","bgColor":"green"}],"textContent":"lot%20","anchorNode":{},"focusNode":{},"selectionId":181862,"yPOS":0}]
但这不支持safari和iOS。
But this wont support safari and iOS.
任何人都可以在这里协助如何将我的数组值与字符串值一起传递给servlet。
Can anyone assist here how to pass my array value to servlet along with string values in same ajax call.
编辑:
这是我试过的更新,
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);
}
this.toJSON = function() {
return {range: this.range};
}
}
在globalSelection中,我有元素以上textSelection。我按照建议添加了toJSON。
In globalSelection, I have the elements of above textSelection. I added toJSON to this as suggested.
现在我在控制台中得到如下结果,
Now I am getting the result in console as below,
[{"range":{}}]
它来了空值...
推荐答案
Javascript提供序列化方法:
Javascript offers methods for serialization:
toJSON()
此函数用于定义应该是什么成为序列化的一部分。基本上,您可以创建要序列化的对象的克隆,不包括循环依赖项或不应发送到服务器的数据。
This function is used to define what should be part of the serialization. Basically you can create a clone of the object you want to serialize excluding cyclic dependencies or data that should not be send to the server.
有关此行为的更多信息,请访问: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior
More information on this behaviour can be found here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior
JSON.stringify()
此函数调用toJSON()并序列化返回的对象。
This function calls toJSON() and serializes the returned object.
示例:
var objectToSend = {};
objectToSend.varX = 5;
objectToSend.varY = 10;
objectToSend.toJSON = function() { return {varX: this.varX}; }
var jsonString = JSON.stringify(objectToSend);
结果将是:
"{"varX":5}"
这篇关于如何使用数组和字符串参数进行Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!