Understand at a glance: jsonp details _ jquery

Source: Internet
Author: User
This article focuses on understanding jsonp at a glance. This article uses easy-to-understand languages to explain jsonp, how jsonp is used, and how jsonp uses instances, if you want to use jsonp, you can refer to json. I believe it is widely used. I have never been able to use jsonp, but I often see that it is "cross-origin ", I never knew what it was. I finally figured it out today. Next, let's figure out what jsonp is.

Same-origin policy

First, for security reasons, the browser has a same-source policy. The same-source policy prevents you from retrieving or setting attributes of documents loaded from one source. It seems that you don't know what it means.

1. create two webpages at will

One port is 2698 and the other is 2701. they are different sources according to definitions.

2. use jQuery to initiate requests with different sources
Add a button on the webpage with Port 2698, and Click the event to initiate two requests to the port 2701 domain.

The code is as follows:


$ ("# GetOtherDomainThings"). click (function (){
$. Get ("http: // localhost: 2701/Scripts/jquery-1.4.4.min.js? 7.1.2 ", function (data ){
Console. log (data)
})

$. Get ("http: // localhost: 2701/home/index", function (data ){
Console. log (data)
})
})


According to the same-origin policy, it is obviously tragic. The browser will stop and will not initiate this request. (Not allowed by Access-Control-Allow-Origin)

OK. in the past, jsonp had to solve this problem.

Cross-Origin capability of script tags
I don't know if you know about CDN. for example, if you use Microsoft CDN, jQuery is not available on our webpage, which is provided by Microsoft's website:

The code is as follows:


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.