Reputation: 2384
I have an api gives result like
Array ( [status] => success
[message] => Data available
[response] => Array (
[0] => Array (
[id] => 1 [name] =>abc
)
[1] => Array (
[id] => 2 [name] => xyz
)
)
)
how can i access each id and name using ajax call in javascript ? I am using below ajax request
$.ajax({
type:'POST',
url: 'myapiurl',
cache: false,
crossDomain: true,
success: function(data){
alert(data);
alert(data.response); //undefined
}
});
Upvotes: 1
Views: 119
Reputation: 587
Your api should return result in JSON rather than array. Once the data is in json you can access it like data.success
, data.message
, etc.
It seems like your api is in PHP, you can use json_encode($data);
Upvotes: 2