I have the need to send an array of ID's to my server-side in the form of a JSON Object.I am using a dropdownlist where multiple values can be selected to do an action with them.
To get them in an array, I've used:
var selectedArray = [];
var selectObj = document.getElementById('addedList');
var i=0;
var count=0;
for(i=0;i<selectObj.options.length;i++){
selectedArray[count] = selectObj.options[i].value;
count++;
}
Now the question is, I need to get those ID's to the server. I've always thought of sending it like a JSON object, since it has a variable amount of parameters. As far as I found out, you can convert a JS object to JSON.
Now I do have a few questions:
Could you give me an example of how to convert it? There seem to be a million ways, one of them being JSON.stringify(jsObj);. My object would simply consist of an array of values. As far as I know this would be an example:
{ array : ["value1","value2","value3"] }
Another question is: How can I send this using jQuery? Can I send a JSON object to the server using $.getJSON? (This uses $.GET under the hood), or do I need to use $.POST ?
Now I've just been trying, but can't get it out...
$.getJSON code
$.getJSON("removerequest.htm",{ ids: JSON.stringify(selectedArray) }, function(data){
$('#removerequestdiv').text('');
$('#removerequestdiv').append('<select name="addedList">');
for(var index in data){
$('#removerequestdiv').append('<option value="' + data[index].id + '">' + data[index].voornaam + data[index].familienaam + '</option>');
}
$('#removerequestdiv').append('</select>');
});