JSON

Posted: June 17, 2010 in JSON
Tags:

JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is primarily used to transmit data between a server and web application, serving as an alternative to XML.

Note: var mydata = eval(‘(‘+myJSONText+’)’); will work. But eval has security issues so use JSON.parse which will throw an exception if the text contains anything dangerous.

JSONP or “JSON with padding” is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server. JSONP is an alternative to a more recent method called Cross-Origin Resource Sharing.

Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is HTML <script> tags. Taking advantage of the open policy for <script> tags, some pages use them to retrieve JSON from other origins

To see how that works, let’s consider a URL that, when requested, returns a JSON statement. In other words, a browser requesting the URL would receive something like:

 {“Name”: “Cheeso”, “Rank”: 7}
The Basic Idea: Retrieving JSON via Script Tags
It’s possible to specify any URL, including a URL that returns JSON, as the src attribute for a <script> tag.

Specifying a URL that returns plain JSON as the src-attribute for a script tag, would embed a data statement into a browser page. It’s just data, and when evaluated within the browser’s javascript execution context, it has no externally detectable effect.

One way to make that script have an effect is to use it as the argument to a function. invoke( {“Name”: “Cheeso”, “Rank”: 7}) actually does something, if invoke() is a function in Javascript.

And that is how JSONP works. With JSONP, the browser provides a JavaScript “prefix” to the server in the src URL for the script tag; by convention, the browser provides the prefix as a named query string argument in its request to the server, e.g.,

 <script type=”text/javascript”
         src=”http://server2.example.com/getjson?jsonp=parseResponse“>
 </script>

The server then wraps its JSON response with this prefix, or “padding”, before sending it to the browser. When the browser receives the wrapped response from the server it is now a script, rather than simply a data declaration. In this example, what is received is

   parseResponse({“Name”: “Cheeso”, “Rank”: 7})

…which can cause a change of state within the browser’s execution context, because it invokes a method.

Read more from here

Advertisements

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