HTML section
File name: test.html
1 <MetaCharSet= "Utf-8">2 <Scriptsrc= "Http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></Script>3 <Script>4 functionJsonpcallback () {5 Alert ('Jsonpcallback');6 }7 $ (document). Ready (function(){8 $.ajax ({9 URL:"http://127.0.0.1/jsonp.php", Ten data: {username:"Admin", Password:"Admin"}, One DataType:"Jsonp", A Jsonp:"Callback", - Success:function(data) { - Alert (json.stringify (data)); the }, - Error:function(XHR, Textstatus, Errorthrown) { - Alert ('Error:' +errorthrown); - } + }); - }); + </Script>
Server section
jsonp.php
1<?PHP2 if(!Empty($_server[' HTTPS ']) && (' on ' = =$_server[' HTTPS '])) {3 $uri= ' https://';4}Else {5 $uri= '/http ';6 }7 $uri.=$_server[' Http_host '].$_server[' Request_uri '];8 9 //gets the function name of the JSONP callback function, which is the value after callback= in the URLTen $jsonp _callback=$_get[' Callback ']; One if($_get[' username '] = = ' Admin ' &&$_get[' password '] = = ' admin '){ A $data=Array(' msg ' = ' Login successful '); -}Else{ - $data=Array(' msg ' = ' User name or password error '); the } - $json= Json_encode ($data); - //returns the callback function name and arguments for the Jsonp - Echo $jsonp _callback.‘ (‘.$json.‘)‘;
jquery uses JSONP cross-domain Request code sample