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: