Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于许多链接,HTTP状态代码返回0_Javascript_Xmlhttprequest - Fatal编程技术网

Javascript 对于许多链接,HTTP状态代码返回0

Javascript 对于许多链接,HTTP状态代码返回0,javascript,xmlhttprequest,Javascript,Xmlhttprequest,这是我的密码 var links = document.getElementsByTagName("a"); for(var i = 0 , max = links.length ; i < max ; i++){ var refLinks = links[i].href; var xhr = new XMLHttpRequest(); xhr.open('GET',refLinks, true ); xhr.onre

这是我的密码

 var links = document.getElementsByTagName("a");
 for(var i = 0 , max = links.length ; i < max ; i++){
       var refLinks = links[i].href;
       var xhr = new XMLHttpRequest();
          xhr.open('GET',refLinks, true );
          xhr.onreadystatechange=function() {
                  console.log("HTTP Status Code:"+xhr.status);
             }
          xhr.send(null);
}
var links=document.getElementsByTagName(“a”);
对于(变量i=0,max=links.length;i
现在在控制台中,我得到了xhr。大多数链接的状态为0,而那些链接没有断开,只有少数链接的状态为200。我试过的网站是


请帮忙。

阅读浏览器的JavaScript控制台

您将看到一条大致如下的消息:

XMLHttpRequest无法加载
http://www.scientificamerican.com/
。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。 runner-3.27.4.min.js:1 HTTP状态代码:0

网站是让用户的浏览器(具有用户的IP地址和cookie)从任意网站获取数据,并使其可供JS使用。这将是一个可怕的安全问题。站点可以为其他站点提供使用JS访问其数据的权限

无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头

但他仍然有一种方法可以用一个简单的PHP脚本“绕过”CORS

以下是HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
    </head>
    <body>
        <div id="myDiv"></div>

        <!-- Script at the end -->
        <script type="text/javascript" >
            var url = "script.php?url=http://www.scientificamerican.com";

            var xhr = new XMLHttpRequest();
            xhr.open('GET', url, true );
            xhr.onreadystatechange = function() {
                // Do ur stuff here
                if (xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("myDiv").innerHTML = xhr.responseText;
                }
            }
            xhr.send(null);
        </script>
    </body>
</html>

var url=“script.php?url=http://www.scientificamerican.com";
var xhr=new XMLHttpRequest();
xhr.open('GET',url,true);
xhr.onreadystatechange=函数(){
//在这里做你的事
如果(xhr.readyState==4&&xhr.status==200){
document.getElementById(“myDiv”).innerHTML=xhr.responseText;
}
}
xhr.send(空);
以下是PHP:

<?php
    // Simple use of file_get_contents function
    function loadHTML($url) {
        return file_get_contents($url);
    }

    // Test if we got parameters
    if(count($_GET) > 0) {

        // Test if url param
        if(isset($_GET['url']) && strlen($_GET['url']) > 0) {
            $html = loadHTML($_GET['url']);

            $param = parse_url($_GET['url']);

            $http_domain = "http://".$param['host'];

            $find = array(
                 'src="'
                ,'href="'
            );

            $replace = array(
                 'src="'.$http_domain
                ,'href="'.$http_domain
            );

            // Why use str_replace
            // Example : In $html, we got : <img src="/image/test.png" />
            //
            // We have to write the domain for using this image
            //
            // After : we got : <img src="http://example.com/image/test.png" />
            // We see the image
            //
            // It work for script, link, img, iframe, etc...
            echo str_replace($find, $replace, $html);
        }
    }
?>