PHP Header and Access-Control_Allow_Origin Error


PHP Header and Access-Control_Allow_Origin Error

我有两个域名,leobee.com和txtease.com。我想从leobee.com加载xml脚本到txtease.com上的php页面。我能够通过研究stackoverflow网站获得创建下面脚本的信息,但是,我没有看到如何解决这个问题。

我已经添加了php头请求脚本,我得到"原点是不允许访问-控制-允许-原点"错误。

你能看看我的脚本,让我知道我哪里出错了吗?测试脚本在:http://txtease.com/crossdomain/scripts/example.html

在chrome/或firebug控制台使用这个:createCORSRequest(‘得到’,' http://www.leobee.com/crossdomain/data/data.xml ');

PHP脚本:

    <?php
echo "PHP Running";
header('Access-Control-Allow-Origin: http://www.leobee.com');
?>
<script>
var xhr = new window.XMLHttpRequest();
var string;
function createCORSRequest(method, url) {

  if ("withCredentials" in xhr) {
    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);
    xhr.send(null);
    string ="with Credentials";
    xhr.onerror = function() {console.log('There was an error!')};
    xhr.onreadystatechange = callbackFunction(string);
  } else if (typeof XDomainRequest != "undefined") {
    xhr = new XDomainRequest();
    xhr.open(method, url);
    xhr.send(null);
    string ="x domain request";
    xhr.onreadystatechange = callbackFunction(string);
  } else if(!"withCredentials" in xhr){
     xhr.open(method, url, true);
     xhr.send(null);
     string ="with no Credentials";
     xhr.onreadystatechange = callbackFunction(string);
  }else {
    // Otherwise, CORS is not supported by the browser.
    alert("cross domain not supported");
    xhr = null;
  }
  return xhr;
}
function callbackFunction(string){
    console.log("Responding function: "+string);
      if (xhr.readyState == 4){
          var responseText = xhr.responseXML;
          console.log("xml string is: "+responseText);
        if (xhr.responseXML ===null){
                responseText=xhr.responseText;
                console.log("html string is: "+responseText);   
        }
      }
    }
</script>

您正在赋予具有原始http://www.leobee.com权限的页面从http://txtease.com/scriptExample.php读取数据,但您需要相反的方式。

读取数据的权限必须来自数据来自的站点。一个站点不能授予自己从任意站点读取数据的权限。

echo "PHP Running";也将输出内容。有输出内容后不能调用header