XMLHttpRequest cannot load http:/server.php.cn/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.php.cn' is therefore not allowed access.
一、允許單個域名存取
指定某域名(http://client.php.cn)跨域存取,則只需在http://server.php.cn/server.php檔案頭部新增如下程式碼:
<?php header('Access-Control-Allow-Origin:http://client.php.cn');
二、允許多個域名存取
指定多個域名(http://client1.php.cn、http://client2.php.cn等)跨域存取,則只需在http://server.php.cn/server.php檔案頭部新增如下程式碼:
<?php $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; $allow_origin = array( 'http://client1.php.cn', 'http://client2.php.cn' );
三、允許所有域名存取
允許所有域名存取則只需在http://server.php.cn/server.php檔案頭部新增如下程式碼:
<?php header('Access-Control-Allow-Origin:*');
以上就是PHP跨域問題解決方案的詳細內容,更多請關注TW511.COM其它相關文章!