0

Trying to populate 2 areas of a section from a JSON response. The response works and outputs the following, however when I try to use data[0] and data[1] to get the title, text and image it doesn't want to. I am getting the data back correctly, just can't seem to add it in.The console.log response is undefined.

JSON Response:

[{"title":"Title 1","text":"Text 1","image":"Image 1"},{"title":"Title 2","text":"Text 2","image":"Image 2"}]

jQuery code:

$.get( 'test-api.php?eventQuery', { answers }, function( data, status ) {
    console.log(data[0].title);
    // Populate both sections with returned data
});
2
  • You're sending it as a string or as a json? Did you tried to use JSON.parse(data)? Commented May 24, 2016 at 13:31
  • Use getJSON instead. Commented May 24, 2016 at 13:32

1 Answer 1

1

Use it like this

$.get( 'test-api.php?eventQuery', { answers }, function( data, status ) {
                console.log(data[0].title);
                    // Populate both sections with returned data


            },"json");

or

$.get( 'test-api.php?eventQuery', { answers }, function( data, status ) {
                data = JSON.parse(data);
                console.log(data[0].title);
                    // Populate both sections with returned data


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

1 Comment

I just solved it myself using: var eventData = JSON.parse(data); console.log(eventData[0].title); Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.