We manage to found ourselves coding something, we make a request from a different server. Most of the times we think "hey! i want sport news! I'll just .load the homescreen from espn.com, then i want sexy women in my site! let's leak out images from maxim", you do this and nothing works... you wonder why...
Imagine if an ajax request could send and load data from any website. You could easilly load personal information from your gmail, facebook and redtu... i mean, youtube. Not a safe path. There are workarounds for this, the basic case is JSONP. Other workaround is a flash proxy, but we'll discuss that one later.
Time to understand JSONP and get down and dirty, in a computer kinda way. JSONP is all about making an ajax query to a server. Imagine you import this script from a different domain...
function thisisajsonp(){ return {"name": "cristian", "age": 23} }
Of course, this content was created dinamically. there you go... that's JSONP, basically. Now let's do something interesting with it.
Twitter has many services available, the ones that involve user account related actions do need some OAuth server side work. Nevertheless the search method is as open as a drunk undergrad chick, and it has a JSONP service.
We can do this http://search.twitter.com/search?q=fml, we tell twitter to search for the "fml" pattern, that's the "q" parameter (for a the full list of parameters click here -> the last here, not this one), Try it.
if do this http://search.twitter.com/search.json?q=fml we retrieve a jsonp format instead. We can loop through this format. The structure of this json object is kinda messy if you try to read it in plain, I recommend exploring it with firebug when we load it later, it's easier to understand. Ok, guns blazing, I'm coding...
click to load!
Firts, the HTML. We have a div called "trigger", when we click there we'll load the tweets to the tweets list. Simple, now the JS... mwahahah!
$(function(){ $("#trigger").click(function(){ $("#tweets").text(""); $("#trigger").text("loading"); $.getJSON("http://search.twitter.com/search.json?callback=?",{q:"fml"},function(data, textStatus){ $.each(data.results, function(i,tweet){ $("#tweets").append("
FYI, you need to know some basic jquery to understand this blog, and be somebody. The method we use is getJSON, we pass some parameters, the URL, the data, and the callback function to handle the data.
This blog is about details, the loopholes in coding that can seriously pull back your schedule (wow, that was like... serious). So, the details here are simple, if you look to the URL you may see i wrote "callback=?", why would I do that? Here's the answer...
down here....
further down....
That way Jquery understands that the format comes from a different domain, and loads the data dynamically. In the first example I showed you how the remote querying could work, a function that returns the data. Functions have a name, if that name is repeated well... it won't work :) so, we can pass the function name, sometimes (most of the time) you don't care what the name of the function is, so it's a pain in the ass to declare a function just for the sake of having something to invoke, jquery does that for you, this function is called "padding". Is like this:
Jquery: hey dude, what's the name of the padding for the data fetched?
You: ?
Jquery: nevermind, i'll handle it.
Just put the "callback=?" in the URL.
The rest of the code is some basic status handling and looping throught the object. Nothing big, as a matter of fact if you click it several times on a row you will load repeated data, it's just an example, we are not building a twitter client here.
If you have any question remember you can google it, you can also follow me @cris7ian, everything is in spanish "tengo un gato en los pantalones" kinda way, but if you contact me I'll answer you un plain english (that's what twitter is all about).