Reputation: 1051
I have refered many questions on stack overflow and on other sites for the solution. But everywhere I got incomplete answer. some answer says log the json data to console. So I tried it and there I get the json object correctly. But when I add it to HTML or alert it says [object Object].
There are many duplicates of this question but still I dont understand what the problem is? Here is my code so far
$(document).click(function () {
$.ajax({
type: "POST",
url: "/members/ListOfAllOppositeTypeUsersWithTheirRespectiveData?LoggedInUserOppositeType=2",
dataType: 'json',
success: function (json) {
console.log(json);
//var strJson = JSON.stringify(json);
$.each(json, function (key, value) {
$('#AllowedFriends').append($('<option value="'+ key + '">' + value + '</option>'));
});
},
error: function () {
alert("F");
}
});
});
edit :
The values that I get in the console :
0: Object
MemberID: 1
Name: "Cipla"
1: Object
MemberID: 2
Name: "Himalaya"
Upvotes: 0
Views: 707
Reputation: 1756
You should try something like:
$.each(json, function (index, obj) {
$('#AllowedFriends').append($('<option value="'+ obj.MemberId + '">' + obj.Name + '</option>'));
});
Upvotes: 1
Reputation: 3881
An object can contain actual data, what is the problem? I suggest you take a look at the network calls from your browser using the developer console or Firebug to see what object is received. Then jQuery parses the string into an object so you can use the data.
Upvotes: 0