Jsonp, short for JSONwithPadding, is a solution for cross-origin request resources. Let's take a look at how it is used.
First glance at jsonp
Jsonp is short for JSON with Padding. It is a solution for cross-origin request resources. Most of the time, we need to obtain server data on the client for operations. Generally, we use ajax + webservice to do this, but if we want to obtain the data and the current page is not a domain, the famous same-origin policy (client scripts in different domains cannot read or write resources of the other party without explicit authorization) will reject requests for security reasons, that is, we cannot directly send requests to other domains to obtain resources.
There is a books. php file in the localhot domain, which contains a script to send a get request to books. php In the test.com domain to obtain its book list resources. This is a cross-origin request resource.
The Code is as follows:
$. Ajax ({
Type: 'get ',
Url: 'http: // test.com/books.php'
});
The page reports an error like this: XMLHttpRequest cannot load http://test.com/books.php. Origin http: // localhost is not allowed by Access-Control-Allow-Origin.jsonp to solve this problem.
Jsonp Principle
Although there are restrictions on the same origin policy, not all HTML resources must be in the same domain. To save traffic or load speed, we use Google or Microsoft's jQuery CDN for common pages, on the page, we can write and reference jQuery.
The Code is as follows: