Hi I’m just trying to do some simple AJAX stuff by retrieving search results from Twitter. It is bringing back the objects but I can’t seem to drill down to the object itself and get the info (such as text).
If I console.log(tweet), it lists all the objects, but the code below just says “undefined”. I’m sure I’m missing something easy.
I’ve read loads of questions/answers on here but none seem to do it. I see many have used the .getJSON() method, but using .ajax() should be ok I would have thought?
$.ajax({
dataType: 'jsonp',
url: 'http://search.twitter.com/search.json?callback=?&q=twitter&rpp=5',
success: function (data) {
$.each(data, function(i,tweet){
console.log(tweet.text);
//$('#twitter ul').append('<li><a href="' + item.text + '">' + item.from_user_id + '</a></li>');
});
}
});
You have to use
data.resultsYou can find a working sample here.
Updated:
I think you are new to javascript and json data format.
The data returned by the request
http://search.twitter.com/search.json?callback=?&q=twitter&rpp=5is in json format. The response for the request contains a key value pair. If you copy paste the request url in your browser you can see the result of the query.It starts with {results: [….]}, it means that the json object contains an array of items which can be accessed used the key
results.Each item in the results array contains the following values
each of these key/values can be accessed with the the
$.each(function(i, tweet){ .... })usingtweet.<key name>. Ex: tweet.from_user_id_str, tweet.text, tweet.source etc…