0

I have the following JSON returned from a remote server:

{
    "success": true,
    "questions": [{
        "id": 23,
        "text": "Is Sky Blue?",
        "response_type": "Multiple Choice",
        "created_at": "2014-12-05T06:37:20.947Z",
        "updated_at": "2015-03-04T00:00:56.915Z",
        "campaign_id": 38,
        "no_repeats": true,
        "max_repeat": 0,
        "yes_no": false,
        "type": "axy",
        "priority": 0
    }, {
        "id": 24,
        "text": "What is your current employment status?",
        "response_type": "Multiple Choice",
        "created_at": "2014-12-05T06:38:56.076Z",
        "updated_at": "2015-03-04T00:00:56.903Z",
        "campaign_id": 38,
        "no_repeats": true,
        "max_repeat": 0,
        "yes_no": false,
        "type": "axy",
        "priority": 0
    }]
}

I am using this code to fetch data from remote URL:

$.get( url, function( data ){
    alert(data.success); // Works
    alert(data. questions); // Returns Blank
});

data.questions returns empty. I tried to eval but it just returns [object]

What's wrong am I doing?

3
  • There is no dataType "json" in your code. Did you skipped it in example? It should not work even for succes if you don't have it in your code Commented Mar 7, 2015 at 22:09
  • @SeriousDron Interesting. I did not put json but it does return success as true Commented Mar 7, 2015 at 22:12
  • 1
    May be you should add it and see what happend. Also you could use console.log instead of alert to get more info. Starting with console.log(data) to see if data is actially what you expecting Commented Mar 7, 2015 at 22:17

1 Answer 1

3

questions is an array. Try

alert(data.questions[0].id)

and you should get 23. If you do, that'll give you the insight you need into access the rest of the data.

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

2 Comments

Already tried that. Didn't work, I already had tried it and it returned Uncaught TypeError: Cannot read property 'id' of undefined
If that is true, then there's something you're missing that you've not shared with us that's causing the problem. As it stands, your JSON is fine and you should be able to access the data object as described - demoed here: repl.it/dHa ... is there any more code involved in this process? Can we access the endpoint that's returning the JSON?

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.