Remote JSON-jsonp

Source: Internet
Author: User

Source: http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/

The browser security model dictates that XMLHttpRequest, frames, etc. must have the same domain in order to communicate. that's not a terrible idea, for security reasons, but it sure does make distributed (service oriented, mash-up, whatever it's called this week) web development suck.

There are traditionally three solutions to solving this problem.

Local Proxy:
Needs infrastructure (can't run a serverless client) and you get double-taxed on bandwidth and latency (Remote-proxy-client ).
Flash:
Remote host needs to deploy a crossdomain. xml file, Flash is relatively proprietary and opaque to use, requires learning a one-off moving target programming langage.
Script Tag:
Difficult to know when the content is available, no standard methodology, can be considered a "security risk ".

I'm proposing a new technology agnostic standard methodology for the script Tag Method for cross-domain data fetching:JSON with padding, Or simplyJsonp.

The way jsonp works is simple, but requires a little bit of server-side cooperation. Basically, the idea is that youLet the client decideOn a small chunk of arbitrary text to prepend to the json document, and you wrap it in parentheses to create a valid JavaScript document (and possibly a valid function call ).

The client decides on the arbitrary prepended text by using a query argument named jsonp with the text to prepend. Simple! With an empty jsonp argument, the result document is simply JSON wrapped in parentheses.

Let's take the Del. icio. us json api as an example. This API has a "script tag" variant that looks like this:

Http://del.icio.us/feeds/json/bob/mochikit+interpreter:

if(typeof(Delicious) == 'undefined') Delicious = {};Delicious.posts = [{    "u": "http://mochikit.com/examples/interpreter/index.html",    "d": "Interpreter - JavaScript Interactive Interpreter",    "t": [        "mochikit","webdev","tool","tools",        "javascript","interactive","interpreter","repl"    ]}]

In terms of jsonp, a document semantically identical to this wocould be available at the following URL:

Http://del.icio.us/feeds/json/bob/mochikit+interpreter? Jsonp = If (typeof (Delicious) % 3d % 3d % 27 undefined % 27) Delicious % 3d % 7b % 7D % 3bdelicious. Posts % 3d

That's not very interesting on its own, but let's say I wanted to be notified when the document is available. I cocould come up with a little system for tracking them:

var delicious_callbacks = {};function getDelicious(callback, url) {    var uid = (new Date()).getTime();    delicious_callbacks[uid] = function () {        delete delicious_callbacks[uid];        callback();    };    url += "?jsonp=" + encodeURIComponent("delicious_callbacks[" + uid + "]");    // add the script tag to the document, cross fingers};getDelicious(doSomething, "http://del.icio.us/feeds/json/bob/mochikit+interpreter");

The fetched URL from this hypothetical experiment wocould look something like this:

Http://del.icio.us/feeds/json/bob/mochikit+interpreter? Jsonp = delicious_callbacks % 5b12345% 5D

delicious_callbacks[12345]([{    "u": "http://mochikit.com/examples/interpreter/index.html",    "d": "Interpreter - JavaScript Interactive Interpreter",    "t": [        "mochikit","webdev","tool","tools",        "javascript","interactive","interpreter","repl"    ]}])

See, because we're re wrapping with parentheses, A jsonp request can translate into a function call or a plain old JSON literal. all the server needs to do differently is prepend a little bit of text to the beginning and wrap the JSON in parentheses!

Now, of course, you 'd have libraries like mochikit, dojo, etc. login acting jsonp so that you don't have to write the ugly Dom script tag insertion yourself, etc.

Of course, this just solves the standardization problem. your page is still toast if the remote host decides to inject malicious code instead of JSON data. however, if implemented, it 'd save a lot of developers some time and allow for generic into actions, tutorials, and documentation to be built.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.