是否有可能使这个代码在多个域上工作而不仅仅是一个[固定问题]

时间:2017-05-28 21:32:10

标签: php jquery ajax cors

由于我已经使用了PHP和JS,我找到了一种方法来使我的脚本工作并且它完美运行

 header("Access-Control-Allow-Origin: *"); (Put this on the PHP page)

https://enable-cors.org/server_php.html

我已经构建了一个脚本来保护内容并且在输入正确的密码之前不显示它但不幸的是在我将其实现到我的用户管理脚本之前我需要它在多个域上工作,即数据库是在我的主域上,用户想要将脚本实现到他们自己的网站上以保护内容,但是当前在不同的域上它只是在输入字段中加载,当输入密码时我得到一个错误。我的代码是

var bustcachevar=1; //bust potential caching of external pages after initial 
request? (1=yes, 0=no)
var bustcacheparameter="";

function createRequestObject(){
try {
    xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new 
ActiveXObject("Microsoft.XMLHTTP");
}   catch(e)    {
    alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
};
return xmlhttp;
};

function ajaxpage(url, containerid){
var page_request = createRequestObject();

if (bustcachevar) bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new 
Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)

page_request.onreadystatechange=function(){
    loadpage(page_request, containerid)
}

}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || 
window.location.href.indexOf("http")==-1)) {

document.getElementById(containerid).innerHTML=page_request.responseText;
};
}


function LoadContent(id, containerid,password) {
ajaxpage("//name.com/identify/load-content.php?id="+id+"&p="+password, containerid);
return false;
}

我的错误是

已被CORS政策阻止:No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://testservername.com'因此不允许访问。

感谢所有帮助赞赏

0 个答案:

没有答案