Cross-domain JSONP, the underlying implementation principle

Source: Internet
Author: User

Sharing the cross-domain is not only because of the increasing number of jobs that are now being encountered, but also often asked during interviews.

So what is cross-domain, let's take a look at the official explanation: The browser is not able to execute scripts from other websites. It is caused by the browser's homologous policy (the so-called homology refers to the domain name, protocol, ports are the same, as long as there is a different cross-domain ), is the browser security restrictions imposed on JavaScript. Here do not focus on the introduction of the Internet has a lot of this aspect of the introduction, this article mainly detailed jsonp of the underlying implementation principle.

Although the browser is limited in this regard, but we can use the script tag, cleverly avoid the same-origin strategy, "rescue", we look at a code

<script>    function  fn (data) {        console.log (data);        } </script><script>    fn ("I'm back-end data"); </script>

Nani??? Isn't this just a normal function call that can't be normal? A lot of small partners must think I put the code wrong, open a joke, this is not an ordinary can not be called in the normal function? Does this have a half-penny relationship with the jsonp you're talking about? I want to say that it is not only related, but also very related, and even this is the underlying implementation of the JSONP principle. You're crazy!!! Don't worry ... Let's keep looking down.

Let me take a look at the Baidu hint used in the cross-domain, he is using the form of JSONP, Https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=hello, (The parameters after the interface I will explain below) This is the Baidu hint of an interface, we put in the browser's address bar to open

Cross-domain JSONP, the underlying implementation principle

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.