I have a web service which has been built to pass Json data. I can verify that the data is being passed and is in the Json format. I have a JavaScript AJAX call which is to connect to the web service and retrieve the data. I can see that this works because I have an alert which shows me the Json data. My dilemma is that I have seen a number of different ways to populate a listbox but having tried a few of them I have not been able to get them to work with my data. I am stumped.
What I have tried:
My Json data:
{ "Batches": [ { "ID": 1, "product": "123117C" }, { "ID": 2, "product": "123116B" }, { "ID": 3, "product": "123116A" }, { "ID": 4, "product": "123017B" }, { "ID": 5, "product": "123016D" }, { "ID": 6, "product": "122917A" }, { "ID": 7, "product": "122916C" }, { "ID": 8, "product": "122817D" }, { "ID": 9, "product": "122817C" }, { "ID": 10, "product": "122816B" } ], "Table1": [ { "id": 0, "item": "item 0" }, { "id": 1, "item": "item 1" } ]}
The HTML code where the listbox resides:
<div class="col-lg-12">
<select path="lstObjects" id="lstObjects" name="BatchID">
</select>
</div>
My Javascript which is to be populating the listbox upon document load:
function GetBatchData() {
$.ajax({
type: 'GET',
url: 'http://192.168.1.103/Web%20Service/Service.asmx/GetBatchData',
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function(product) {
var products = JSON.parse(product.d);
$.each(products, function(index, value) {
$('#lstObjects').append($('<option>').text(value).val(index));
});
},
failure: function(error) {
alert(error.d);
}
});
};
Right now this Javascript does not have an attempt to populate the listbox. The closest I got to doing that took all the records from the Json call and paste them into one line in the listbox. My ultimate goal with this project is to allow the user to select a listbox item and that item would be associated with a variable in the Javascript to update a plot function I have.