Tuesday, 27 August 2013

how to send dynamic data using ajax

how to send dynamic data using ajax

normally, we have a ajax request like:
$.ajax({
type: 'GET',
url: "/biboundsoptimization",
data: {
objects: '2',
},
success: function( data ) {
console.log(data);
alert(data);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+"
er:"+er);
}
});
but now I cannot know the number of variables in the data field . For
example, when the client enters 2 the data field should be like:
data: {
objects1: value of objects1,
objects2: value of objects2,
},
if the client enters 3 the data field should be like:
data: {
objects1: value of objects1,
objects2: value of objects2,
objects3: value of objects3,
},
Before the ajax request, the client has already entered the value of each
objects into different input fields. But i don't know the number until he
enters it. So i cannot write as above. it is obvious that a loop is
required to get all the objects by looping over the number of loops
provided by the client. I tried to something like:
for(int i = 1; i < clientValue; i++){
'objects' + i: $('#objects' + i).val(),
}
But it doesn't work. Grammatically it is wrong. Can anyone help me with
that? Thanks!

No comments:

Post a Comment