admin管理员组文章数量:1410682
I have an key value pair array which I need to send to another function through ajax. My array looks something like this
var vitals=new Array();
var vitals["height"]=170;
var vitals["weight"]=55;
the ajax function is
$.ajax({
type: "POST", //GET or POST or PUT or DELETE verb
url: url, // Location of the service
data: JSON.stringify({ccdEntity: vitals }), //Data sent to server
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
}
and the function receiving the value is
public bool GenerateCcd( Array ccdEntity)
when I run the program the function does not display the array with key-values but when I transfer a normal array (i.e) vitals[0]=170, it works fine . Do I need to make an changes for sending the above data to function?
Edit: Tried passing the above array as a JSON object
var vitals= {
"height": "170",
"weight": "55"}
but results still the same
I have an key value pair array which I need to send to another function through ajax. My array looks something like this
var vitals=new Array();
var vitals["height"]=170;
var vitals["weight"]=55;
the ajax function is
$.ajax({
type: "POST", //GET or POST or PUT or DELETE verb
url: url, // Location of the service
data: JSON.stringify({ccdEntity: vitals }), //Data sent to server
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
}
and the function receiving the value is
public bool GenerateCcd( Array ccdEntity)
when I run the program the function does not display the array with key-values but when I transfer a normal array (i.e) vitals[0]=170, it works fine . Do I need to make an changes for sending the above data to function?
Edit: Tried passing the above array as a JSON object
var vitals= {
"height": "170",
"weight": "55"}
but results still the same
Share Improve this question edited Mar 13, 2014 at 5:16 Rohit Ranjit asked Mar 13, 2014 at 4:59 Rohit RanjitRohit Ranjit 171 silver badge6 bronze badges 2-
1
Change the
vitals
declaration to bevar vitals = {};
. That way you can correctly use a key/value pair structure. Using an array, you were setting properties that aren't serialized. An object ({}
) is really what you're looking for – Ian Commented Mar 13, 2014 at 5:01 - @Ian Thanx for the reply.Tried that change but values still not getting passed. – Rohit Ranjit Commented Mar 13, 2014 at 5:06
2 Answers
Reset to default 2Make your vitals
an object array rather than an array;;
var vitals={'height': '170', 'weight': '55'};
And post your data like:
data: JSON.stringify(vitals)
Use something like this::
function TestAjax() {
var vitals= [];
for (var i = 0; i < 5; i++) {
vitals.push({ Height: (170+i), Weight: (55+i) });
}
$.ajax({
type: 'POST',
url: url,
contentType: "application/json",
data:JSON.stringify( {vitals: vitals}),
success: function (data) {
alert("Succeded");
}
});
}
本文标签: javascriptPassing KeyValue Array AJAXStack Overflow
版权声明:本文标题:javascript - Passing Key-Value Array AJAX - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744939342a2633388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论