0

Essentially what I'm doing is trying to pull in Twitter data for use in Javascript app. I have a php file which is calling the API and retrieving JSON data, but I can't seem to access this data within my javascript file.

I'm trying to use this method.

$.getJSON("tweets_JSON.php?count=1&callback=listTweets");

Then to test it I'm using...

console.log(listTweets[0].id_str);

This returns a message that listTweets isn't defined. I've tried removing the callback form the url and declaring it as

var listTweets = $.getJSON("tweets_JSON.php?count=1&callback=listTweets"); 

This seems to access the first object in the array, but then I get an error saying the object property I'm looking for (.id_str) can't be found.

1 Answer 1

2

Whenever you add a callback parameter, jQuery expects there to be an actual function with that name that is called

$.getJSON("tweets_JSON.php?count=1&callback=listTweets");

function listTweets(json) {

    console.log(json)

    // called when ajax call is completed
}

If you don't want that, and you're doing a call to your own server, just rename the GET parameter

$.getJSON("tweets_JSON.php?count=1&cbk=listTweets").done(function(json) {

    console.log(json)

    // called when ajax call is completed
});

Also, console.log(listTweets[0].id_str); does nothing, listTweets would be the name of a function, not some variable you can access later ?

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

5 Comments

Awesome. Thanks for your help. Your first method seems to work (at least it doesn't kick an error). I guess my follow up question would be how would you go about accessing the object properties like id_str? Sorry if that's a stupidly basic question. I'm new to programming.
You would do that inside the function, json would be the returned data, and you can see it by logging it to the console, as in console.log(json)
Hmm, so it should look something like... $.getJSON("tweets_JSON.php?count=1&callback=listTweets"); function listTweets(json) { console.log(json); }; listTweets(); This is returning undefined?
Why not just remove the callback parameter if you're not going to use a callback function, just rename it to something else and use the second code snippet above
Because that would work, and I wouldn't be able to annoy benevolent strangers on the internet any longer. : ) I see the data now. Thanks so much for taking the time to respond. Very much appreciated.

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.