Javascript:
<script>/* @desc loading xhr file @author Lee [<[email protected]>] @param file path @param async synchronous or asynchronous True Async Flase sync @return xmldoc loaded content */function Loaddoc (file,async=true) {if (window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest ();} else{//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP");} if (async = = = True) {Xmlhttp.onreadystatechange = function () {if (Xmlhttp.readystate < 4) {//load in }else if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) {//successful VAR xmldoc if (Xmlhttp.getresponseheader (' content-type ') = = = = ' Application/json ') {xmldoc = Json.parse (xmlhttp.responset EXT); }else{xmldoc = xmlhttp.responsetext} return xmldoc}else{//Failed Xmlhttp.abort () return}}}xmlhttp.open ("POST", File,async); Xmlhttp.setrequestheader ("Conten T-type "," Application/json "); var data = {Name:" Lee "}var str = json.stringify (data) xmlhttp.send (str); if (async = = = False) { var xmldoc if (Xmlhttp.getresponseheader (' content-type ') = = = ' Application/json ') {xmldoc = Json.parse (xmlhttp.res Ponsetext); }else{xmldoc = xmlhttp.responsetext} return xmldoc}}var str = loaddoc (' test.php ', false) Console.log (str.name ) </script>
Php:
<?phpheader("content-type:application/json");$json = file_get_contents(‘php://input‘);echo $json;
JavaScript and PHP use JSON for data communication