Here’s the JSON string returned from my action:
[{"Key":"Likes","Value":1},{"Key":"Loves","Value":0},{"Key":"Dislikes","Value":0},{"Key":"Message","Value":"Your vote has been changed"}]
Here’s how I’m trying to access them:
$.ajax({
type: "POST",
url: '/voteUrl',
data: { id : '37', vote : $(this).attr('id') },
dataType: "json",
success: function(result) {
$('#like').text(result.Likes);
$('#love').text(result.Loves);
$('#dislike').text(result.Dislikes);
}
});
There are span tags with id’s “Like”, “Love”, and “Dislike.” On the action, there is a dynamic result, with properties like so:
results.Likes = 1;
results.Loves = 0;
results.Dislikes = 0;
results.Message = "Your vote has been changed";
But the span tags aren’t being updated with the correct values. The ajax call is working, FireBug shows the JSON string is returned, and I believe I’m trying to access the keys correctly, results.PropertyName. What’s missing?
You are returning an array, so to show all, you have to use
forloop. Also in your current code, you are not accessing array by key. The correct code could be:or if you have fixed length, than get the value directly instead of forloop or by comparing the key if you want to use
forloop