Have a chat JSONP

Source: Internet
Author: User

JSONP

When it comes to JSONP, it's about homologous strategy (same origin policy), which is the most central and basic security feature of the browser.

The browser's same-origin policy restricts the "document" or script from different sources to read or write to the current "document" or set certain properties.

The popular argument is that foreign scripts cannot touch their own documents.

There are four factors that affect the source: hostname host, subdomain, port, protocol.

JSONP (JSON with padding). This is a way to fetch data from a remote server. The principle is that by creating a script tag, the external file contains a piece of JSON data that is returned by the server and wrapped in a function call as a parameter. Script tags get scripts files that are not subject to cross-domain restrictions (as well as labels that have the ability to load data across domains: IMG, iframe, link, and so on are not restricted by the same-origin policy), and all browsers support this technology.

First, add a script tag to the head.

<script src="Http://example.com/data.json"></script>

Pre-defined global functions, waiting for method calls in the returned data

 window.jsoncallback = function (Result) {//handles the related logic that returns the result}  
">window . Jsoncallback = function {//related logic for returning results }

The data in the requested file is a method that has data as a parameter, the script is executed after the data is loaded into the browser, and the method is called (the method is already defined)

jsonCallback({"data":"foo"});

Fortunately, JQ has wrapped it up.

Jquery.getjson ("Http://example.com/data.json", function() {//Handle related logic for returning results})

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.