Cross-domain access is simple by setting access-control-allow-origin.
For example: The domain name of the client is www.client.com, and the requested domain name is www.server.com
If you use AJAX access directly, you will get the following error
XMLHttpRequest cannot load http://www.server.com/server.php. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' http://www.client.com ' is therefore not allowed access.
Join in the requested response header
[PHP]
1 //Specify to allow other domain names to access the 2 header (' access-control-allow-origin:* '); 3 //Response type 4 header (' Access-control-allow-methods:post '); 5 //Response header set 6 header (' Access-control-allow-headers:x-requested-with, Content-type ');
You can implement Ajax post cross-domain access.
The code is as follows:
client.html Path: http://www.client.com/client.html
[HTML]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"> <HTML> <Head> <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"> <title>Cross-domain Testing</title> <Scriptsrc= "//code.jquery.com/jquery-1.11.3.min.js"></Script> </Head> <Body> <DivID= "Show"></Div> <Scripttype= "Text/javascript">$.post ("http://www.server.com/server.php", {name:"Fdipzone", Gender:"male"}). Done (function(data) {document.getElementById ("Show"). InnerHTML=Data.name+ ' ' +Data.gender; }); </Script> </Body> </HTML>server.php Path: http://www.server.com/server.php[php] View plain copy<?php $ret = array (' name ' = = isset ($_post[' name ') ')? $_post[' name ']: ', ' gender ' = isset ($_post[' gender ')? $_post[' gender ']: '); Header (' Content-type:application:json;charset=utf8 '); Header (' access-control-allow-origin:* '); Header (' Access-control-allow-methods:post '); Header (' Access-control-allow-headers:x-requested-with,content-type '); echo Json_encode ($ret); ?>
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.client.com ');
If you need to set multiple domain names to allow access, you need to use PHP to deal with
For example, allow www.client.com and www.client2.com to be accessed across domains
server.php revision changed to
[PHP]
1<?PHP2 $ret=Array( 3' Name ' + =isset($_post[' name '])?$_post[' Name ']: ',4' Gender ' =isset($_post[' Gender '])?$_post[' Gender ']: '5 ); 6 7 Header(' Content-type:application:json;charset=utf8 '); 8 9 $origin=isset($_server[' Http_origin '])?$_server[' Http_origin ']: '; Ten One $allow _origin=Array( A' Http://www.client.com ', -' Http://www.client2.com ' - ); the - if(In_array($origin,$allow _origin)){ - Header(' Access-control-allow-origin: '.$origin); - Header(' Access-control-allow-methods:post '); + Header(' Access-control-allow-headers:x-requested-with,content-type '); - } + A EchoJson_encode ($ret); at?>
Ajax cross-domain access