Providing Context to a JSON Callback

My twitter search engine prototype is going smoothly. But as I wanted to add more complex features, I ran into an issue. Twitter Search API provides JSON support with a callback. I like this option very much, as it allows doing all the work on the browser. There is no need for browser to my web server to twitter web server communication. Instead, the user’s browser directly interacts with twitter’s server and can process the results in the browser itself.

So, the way to specify a callback api is to pass the name of the function to the Twitter API, something like

http://search.twitter.com/search.json?callback=foo&q=some-query

Then, the response would be

foo(results-represented-as-JSON-object)

So far so good. Now I wanted to keep track of some context within which the search request is being made. So, I first created an object to represent the context, defined the callback routine within that object. Now, since I am going to have several requests made, I need to have different context objects. So, I created a context object array and then I created a request URI like

http://search.twitter.com/search.json?callback=cArray%5B0%5D.foo&q=some-query

the next time a request needs to be made, it would be cArray[1].foo and so on.

However, the response that was sent is invalid. It’s like

cArray([0].foo(JSON))

I have no idea how it ended up like this.

So, I immediately looked at Google’s own API to see what it does. Interestingly, in their search API they take two separate parameters, one for callback and one for context. Their example is

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton&callback=foo&context=bar

I wanted to see if my way of using a callback function which is actually member function of an array object worked or not. I tried

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton&callback=myarray%5B0%5D.foo

and the response is

{“responseData”: null, “responseDetails”: “bad or missing callback or context”, “responseStatus”: 400}

To me this feels completely wrong. The workaround I am using for now in my twitter prototype is to create one variable for each context at the document level scope (by using eval) and then passing that in the callback. Something like

http://search.twitter.com/search.json?callback=c0.foo&q=some-query

where c0 is a new variable created for the first request. Next request would introduce c1 and so on.

Now the 20 lines question is, shouldn’t the callback name be allowed to be something like contextObjArray[someindex].callback as per JSON spec? Or, are these all wrong implementations?

Advertisements

Leave a comment

Filed under javascript, JSON

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s