Jsonp ways to get JSON data across domains __JS

Source: Internet
Author: User
Tags script tag domain server

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)

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.