我有两个域名,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