http://www.iteye.com/topic/169765
Have no time to play JS, see the Open API Douban network, after checking some documents, understand how to JSONP cross-domain request. Share it with you.
In fact, JSONP is a very simple thing.
The main use of the <script/> tag for JavaScript document dynamic analysis to achieve. (You can actually use the Eval function)
Here's a very simple example:
The first is to prepare the client's code,
HTML code <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Where Jsoncallback is registered by the client, gets the function of the callback after the JSON data is on the Cross-domain server.
Http://crossdomain.com/jsonServerResponse?jsonp=jsonpCallback
This URL is an interface for JSON data across a domain server, the name of the callback function, and the returned format
JS code jsonpcallback ({msg: ' This is JSON data '})
Jsonpcallback ({msg: ' This is JSON data '})
Jsonp principle:
First register a callback on the client, and then pass the callback name to the server.
At this point, the server is being JSON data.
Then, in JavaScript syntax, a function is generated, and the function name is the parameter Jsonp passed up.
Finally, the JSON data is placed into function directly in the form of a parameter, which generates a document of JS syntax and returns it to the client.
The client browser parses the script tag and executes the returned JavaScript document, where the data is passed as a parameter into the client's predefined callback function. (Dynamic execution of callback functions)