Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  145] [ 1]  / answers: 1 / hits: 7208  / 11 Years ago, sat, december 28, 2013, 12:00:00

I am new to jQuery. I am creating the json string in my servlet using gson-2.2.3.jar:



Gson gs = new Gson();
String json = gs.toJson(names);
System.out.println(json);
PrintWriter out = resp.getWriter();
out.println(json);
out.close();


Below are the two commented codes I tried for binding the json string with drop-down but none of them works:



$.ajax({
type:POST,
url: DropDownController,
success: function(result){
alert(result);
/* for (var id in result) {
$(#drpDown).append('<option value='+id+'>'+id+'</option>');
} */
/* $.each(result, function (index, value) {
$(#drpDown).append('<option value='+value.id+'>'+value.name+'</option>');
}); */
}
});


The alert message box present in the success displays the json string in below format:



[Alabama,California,Alaska,Ohio]



Kindly let me know how to bind the above json string data with the drop-down.


More From » jquery

 Answers
2

Try like this:



$.ajax({
type: 'POST',
url: 'DropDownController',
dataType: 'json',
success: function(result) {
$.each(result, function() {
$(#drpDown).append(
$('<option/>', {
value: this,
html: this
})
);
});
}
});

[#49171] Friday, December 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raynamadilynl

Total Points: 653
Total Questions: 110
Total Answers: 98

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
;