dongyang7152 2014-10-30 19:32
浏览 69

如何解决这个XMLHttpRequest:没有访问控制允许原点,[重复]

This question already has an answer here:

I have a couple of clients that hire me for hosting and maintenance of their site. To facilitate my work , I 'm adding a js that put their site down* when they do not pay me. Now I do it manualy by ftp.

*(By css y put all the divs in display none)

This is the js that goes on the page:

$(document).ready(function() {
    getOutput();
});

function getOutput() {
  getRequest(
  'http://www.mypage.com/test.php', 
   drawOutput  
);
return false;
}  

function drawOutput(responseText) {

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = responseText;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);

}
function getRequest(url, success) {
var req = false;
try{
    req = new XMLHttpRequest();
} catch (e){
    try{
        req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            return false;
        }
    }
}
if (!req) return false;
if (typeof success != 'function') success = function () {};
if (typeof error!= 'function') error = function () {};
req.onreadystatechange = function(){
    if(req .readyState == 4){
        return req.status === 200 ? 
            success(req.responseText) : error(req.status)
        ;
    }
}
req.open("GET", url, true);
req.send(null);
return req;
}

this is the php:

$array = array(
    "test"  => "up",
    "test2"     => "down",
    "test3" => "down",
);

if(isset($_SERVER['HTTP_REFERER'])) {

    $sitio = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); //i get the url

    $verifico = explode(".", $sitio); // divide the url

    if ($verifico[0] == "www"){ //i check if it has www or not
        $sitio = $verifico[1];
    }
    else {
        $sitio = $verifico[0];
    }
}

if ($array[$sitio] == "down"){
    echo 'my.js';
}

This is the js code that print after the php request:

$(document).ready(function() {
var jodita = '<style type="text/css">body,div{display:none!important;}</style>'; 
document.getElementsByTagName('head')[0].innerHTML = jodita;
});

This is how it works, the js makes a request to the php. The php take the url and verify if the site must be up or down and return the url of a js depending the result. I make it this way because in the future I can change the second js without touching all the sites.

This works fine when I use it in the same domain, but when I use a diferent one give me this error:

XMLHttpRequest cannot load http://domain.com/myfile.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.theotherdomain.com' is therefore not allowed access.

I´ve try some solutions like CORS, but it is to dificult and I don´t get it. There is another option?

</div>
  • 写回答

1条回答 默认 最新

  • donglei_0507 2014-10-30 19:43
    关注

    Like the error messages says, you need to add the Access-Control-Allow-Origin header to your PHP script:

    //Allow Ajax requests from any domain
    header("Access-Control-Allow-Origin: *");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭