I have the following jQuery code. I am able to get the following data from server [{"value":"1","label":"xyz"}, {"value":"2","label":"abc"}]. How do I iterate over this and fill a select box with id=combobox
$.ajax({
type: 'POST',
url: "<s:url value="/ajaxMethod.action"/>",
data:$("#locid").serialize(),
success: function(data) {
alert(data.msg);
//NEED TO ITERATE data.msg AND FILL A DROP DOWN
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
},
dataType: "json"
});
Also what is the difference between using .ajax and $.getJSON.
This should do the trick:
Here’s the distinction between
ajaxandgetJSON(from the jQuery documentation):EDIT: To be clear, part of the problem was that the server’s response was returning a json object that looked like this:
…So that
msgproperty needed to be parsed manually using$.parseJSON().