1

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
        }
    });
Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31
Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56

1 Answers1

2

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);

kero
  • 10,647
  • 5
  • 41
  • 51
Somil
  • 567
  • 5
  • 13