This article, by settingAccess-Control-Allow-Originachieves cross-domain.
For example: the client's domain is client.example.com, while the requested domain is server.example.com.
If you access it directly using ajax, the following error will occur:
XMLHttpRequest cannot load http://server.example.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.example.com' is therefore not allowed access.
1. Allow a single domain to access
To allow a specific domain (http://client.example.com) cross-domain access, simply add the following code at the top of the http://server.example.com/server.php file:
header('Access-Control-Allow-Origin:http://client.example.com');
2. Allow multiple domains to access
To allow multiple domains (http://client1.example.com, http://client2.example.com, etc.) cross-domain access, simply add the following code at the top of the http://server.example.com/server.php file:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
'http://client1.example.com',
'http://client2.example.com'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}
3. Allow all domains to access
To allow all domains to access, simply add the following code at the top of the http://server.example.com/server.php file:
header('Access-Control-Allow-Origin:*');