1. What is JSONP?
JSONP (JSON with Padding) is an unofficial protocol that allows the server-side integration of script tags back to the client to achieve cross-domain access in the form of JavaScript callback (this is simply a JSONP implementation form).
2. What is the use of JSONP?
Due to the limitations of the same-origin policy, XMLHttpRequest only allows resources to request the current source (domain name, protocol, port), in order to implement cross-domain requests, cross-domain requests can be implemented through the script tag, and then output JSON data on the server and execute callback functions to resolve cross-domain data requests.
3, how to use JSONP?
The demo below is actually a simple representation of JSONP, after the client declares the callback function, the client requests data across the domain through the script tag, then the server returns the corresponding data and executes the callback function dynamically.
HTML code (either):
HTML code
- <Meta content="text/html; Charset=utf-8 " http-equiv="content-type " />
- <script type="Text/javascript">
- function Jsonpcallback (Result) {
- alert (result);
- for (var i in result) {
- Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc.
- }
- }
- var jsonp=document.createelement ("script");
- jsonp.type="Text/javascript";
- jsonp.src="Http://crossdomain.com/services.php?callback=jsonpCallback";
- document.getElementsByTagName ("Head") [0].appendchild (JSONP);
- </Script>
HTML code
- <Meta content="text/html; Charset=utf-8 " http-equiv="content-type " />
- <script type="Text/javascript">
- function Jsonpcallback (Result) {
- alert (RESULT.A);
- alert (result.b);
- alert (RESULT.C);
- for (var i in result) {
- Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc.
- }
- }
- </Script>
- <script type="Text/javascript" src= "http://crossdomain.com/services.php?callback= Jsonpcallback "></script>
JavaScript links must be under function.
Server PHP Code (services.php):
PHP code
- <?php
- The service side returns JSON data
- $arr =Array (' A ' =>1,' B ' =>2,' C ' =>3,' d ' =>4,' e ' =>5);
- $result =json_encode ($arr);
- Echo $_get[' callback ']. ' ("hello,world!") ';
- Echo $_get[' callback ']. " ($result) ";
- Dynamic Execution callback function
- $callback =$_get[' callback ');
- echo $callback." ($result) ";
If the above JS client code is implemented in the JQuery method,
$.getjson
$.ajax
$.get
How to implement the client JS code in jquery 1:
JS Code
- <script type="Text/javascript" src="jquery.js" ></script>
- <script type="Text/javascript" >
- $.getjson ("http://crossdomain.com/services.php?callback=?",
- function (Result) {
- For (var i in result) {
- Alert (i+":" +result[i]); Loop output a:1,b:2,etc.
- }
- });
- </script>
Jsonp principle:
First register a callback with the client and then pass the callback name to the server.
At this point, the server becomes JSON data.
Then, in JavaScript syntax, a function is generated, and the function name is the parameter Jsonp passed up.
Finally, the JSON data is placed directly into the function in the form of a parameter, so that a document of JS syntax is generated and returned to the client.
The client browser parses the script tag and executes the returned JavaScript document, where the data is passed in as a parameter to the client's pre-defined callback function. (Dynamic execution callback function)
AJAX cross-domain request-JSONP get JSON data