Related articles: PHP JSON data creation and resolution program code
JSONP (JSON with Padding) is an unofficial protocol that allows the server-side integration script tags to be returned to the client and Cross-domain access through JavaScript callback (this is simply a JSONP form of implementation).
Client-side HTML code:
The code is as follows |
Copy Code |
<meta content= "text/html; Charset=utf-8 "http-equiv=" Content-type "/> <script type= "Text/javascript" > function Jsonpcallback (Result) { alert (result); for (var i on 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> |
Service-Side PHP code
The code is as follows |
Copy Code |
<?php
Server 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 of callback functions $callback =$_get[' callback ']; echo $callback. " ($result) ";
?> |