Because of the browser-homologous policy, a domain name of JS to the B domain name Ajax request will be banned. JS implementation across the Access interface has 2 methods.
1. The backend interface is set to allow spanning header headers.
Header (' access-control-allow-origin:* '); Support for full domain access, insecure, after deployment requires a fixed limit to the client URL header (' access-control-allow-origin:http://www.example-a.com '); The URL with the protocol is set, not a domain name. You can set multiple URLs, separating echo json_encode with commas ([' name ' = ' Lucy ']);}
The client will be able to request normal Ajax.
2. The client passes the JSONP request, the server does not have to set across the header. About JSONP The principle is no longer said here, you can find information separately. Here is a concrete way to implement it.
client requests, as examples of jquery implementations
<script type= "Text/javascript" > $ (document). Ready (function () { $.ajax ({ type: "Get", async: False, URL: "http://www.example-a.com/api.php?id=1", dataType: "Jsonp", Jsonp: "Callback",//Request PHP parameter name //jsonpcallback: "Jsonhandle",//callback function to execute, (in jquery, this parameter can be omitted, jquery randomly generates a name) success:function (data) { console.log (data);}); }); </script>
api.php
<?php //Complete business logic to get Data echo $_get[' callback ']. ' ('. Json_encode ($data). ') '; $_get[' callback ', which is the ' Jsonp ' parameter of Jsonp request in jquery above
?>
JS spanning request 2 implemented JSONP/backend interface settings run across headers