Meant to post something about this the other night, when I got distracted by the latest Questionable Content. First, a shout to bob.pythonmac.org, which proposes an ad-hoc standard for JSON Padded requests (JSONP). Like most interesting and useful things on the web, it was proposed two years ago, and only now have I run across it in places like the Facebook API docs at developer.facebook.com.
Looking at this, I realized that problem being addressed is exactly the same as one I have wrestled with for some time. Script includes, and JSON metadata in particular, are impeded in terms of usefulness by the difficulty in getting the contents to actually do what you want them to. Pure JSON merely states values, it doesn’t make assignments, necessitating an arbitrary wrapper of some kind that spoon-feeds the data into the page (and is not itself valid JSON). By a similar virtue, JavaScript containing var and function statements can be fruitful, but they all get thrown into the global namespace unless you mask them with a Crockford wrapper or specify an existing object to bind them to. In very few other programming environments do people have to settle for this kind of repetitious nonsense:
mystuff = window.mystuff || {};
mystuff.newmethod = function() {};
mystuff.newclass = {
“some”:null,
“more”:null,
“bull”:null,
“shit”:null
};
It’s egregious and disgusting. But for cross-domain applications and quick scripting hacks you usually don’t have much choice.
The beauty of JSONP (which, due to my earliest computer science learnings and my familiarity with the Jargon File, always makes me think JSON predicate (to JSON, or not to JSON?)) is that it allows the browser to continue to interpret JavaScript at this level, while allowing backend services and stored JSON objects to ignore the extra fluff. To supply the fluff, you call a REST-style web service on top of the JSON service you want; and you hand it an extra argument specifying some code to prefix to the response.
I did something very similar once at work, and it seemed pretty slick. This proposal avoids some of the wrinkles, but at the same time I wonder if it might be too simple to capture the power of the idea. Prepending code is very URL-hack-friendly (not a bad thing in this case) but it also feels disorganized. Six months ago I probably wouldn’t have seen anything wrong with it, but here’s the thing. A standard is most useful when it not only does what people need it to do, it suggests its proper place in the scheme of things.
In this case, while it may be cool that you could shove an alert() statement (or whatever the hell else you like) in there before the code runs, chiefly that’s not what it’s for. You prepend so you can assign the returned value somewhere; you wrap so that you can run in a more controlled environment, or supply your object to a function. Basically, any time you want to use something like JSONP you’ll be trying to do one of a short list of things:
- Insert debugging statements or package manager callbacks.
- Prepare a namespace for a JavaScript “class”, or otherwise assign the received value onto some location.
- Call a function that is returned from the server, or call an existing function on data that is returned from the server.
- Roll-up a set of JavaScript classes into one request
- Something silly.
Given this, I’m not going to try and make a counter-proposal now. But I think this could argue for something slightly larger than JSONP, so that you get to fully harness REST and really get the most out of your stored scripts. It’s something that would be particularly interesting in combination with the class parser idea I suggested earlier–real classes, parsed out by a more robust language like PHP and returned as consistently correct JavaScript that smoothly integrates with the rest of your Ajax code.
Akismet I’ll have to get back to, as I should probably be catching a train nowish. Basically I was really, really confused when I first looked at the user interface, and I’m still not 100% sure I fully grok what it’s doing.
