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
        }
    });
4
  • 1
    what you have there is a string m'a men Commented Mar 18, 2016 at 10:50
  • 1
    never seen a api that returns a string of a php array Commented Mar 18, 2016 at 10:54
  • actually in string there are passing some dutch language character which are not supported in json format Commented Mar 18, 2016 at 11:21
  • 1
    @Kirankumar Maybe this post helps? Commented Mar 18, 2016 at 12:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.