Reputation: 673
i have callback of jquery ajax call with json result as:
function (data) {
alert(data);
alert(data['169874']);
}
and:
alert(data) shows: {"169874":"123"}
but:
alert(data['169874']) shows undefined.
why is it undefined?
Upvotes: 2
Views: 3736
Reputation: 235962
You need to JSON.parse()
the JSON string before accessing it as Javascript Object:
data = JSON.parse(data);
This is done automatically by jQuery, if you specify the dataType json
in a $.ajax()
help call. This is also done implicite by $.getJSON()
help for instance.
Upvotes: 8