First, implement get method cross-domain Request data
Browser-side
<Script>$ (document). Ready (function() {$.ajax ({URL:"http://www.xxx.cn/index.php", type:"Get", DataType:"Jsonp", //Specifies the type of data returned by the serverSuccess:function(data) {Console.log (data); } });});</Script>
Server-side
<?php$data = [' info ' = ' cross-domain request succeeded ', ' status ' = 1]; $callback = $_get[' callback '];// The callback parameter is a callback parameter that initiates a request that is jquery. echo $callback. ' ('. Json_encode ($data). ') '; /All return data is required in this manner.?>
Second, the implementation of the post way cross-domain request
Browser-side
<script>$ (document). Ready (function () {$.ajax ({ URL: "http://www.xxx.cn/index.php", type: "POST", dataType: "JSON",//Specifies the type of data returned by the server success:function (data) { console.log (data);});} ); </script>
Server-side
access-control-allow-origin:* that allows any domain name to be accessed across domains
If you need to specify a domain name to allow cross-domain access, simply change the access-control-allow-origin:* to access-control-allow-origin: allowed domain name
For example: Header (' access-control-allow-origin:http://www.xxx.com ');
<?phpheader (' access-control-allow-origin: * ');//Allow all sources to access the header (' access-control-allow-methods:post ');//Response type $ data = [' info ' + ' post method, cross domain request succeeded ', ' status ' = 1];echo Json_encode ($data);? >
Some header request headers
Specify allow other domain names to be accessed
Header (' access-control-allow-origin:* ');
Response type
Header (' Access-control-allow-methods:post ');
Response Header Settings
Header (' Access-control-allow-headers:x-requested-with,content-type ');
//设置内容类型为json
Header (' Content-type:application:json;charset=utf8 ');
jquery, JavaScript implementation (GET, post two ways) cross-domain solution