我的javascript结构出了什么问题

what is wrong with my javascript structure

本文关键字:什么 问题 javascript 结构 我的      更新时间:2024-06-09

我对javascript真的很陌生。我想从url中读取xml,并在html上解析它。我有这样的html和javascript代码:

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
     <script>
       function loadXMLDoc(filename)
        {
          if (window.XMLHttpRequest)
             {
              xhttp=new XMLHttpRequest();
             }
          else // code for IE5 and IE6
              {
                  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
          xhttp.open("GET",filename,false);
          xhttp.send();
          return xhttp.responseXML;
        } 
        xmlDoc=loadXMLDoc("http://www.w3schools.com/dom/books.xml");
        x=xmlDoc.getElementsByTagName("title")[0];
        y=x.childNodes[0];
        document.write(y.nodeValue);
    </script>
    </body>
    </html>

怎么了?感谢

这是完成此任务的标准方法:

由于人们一直在尝试将跨域用于JSONP之外的东西它不会起作用

下面的代码是一个工作示例,因为您的服务器可以从其更受控制的其他网络位置获取内容。。另一方面,您的浏览器只能接收JSONP或PLAIN文本。。。大多数谷歌搜索结果也应该解释这一点。。

您唯一的选择

是使用某种形式的PROXY来获取您试图访问的内容,因此您在这里只有三个选择。

  • 使用JSONP或纯文本
  • 使用服务器/网站/脚本/页面本地的代理或其他方法
  • 在被告知适用跨域规则后,请继续尝试使用此处发布的示例

JAVASCRIPT:

function loadXMLDoc(sURL) {
    $.post( "myproxy.php", { requrl: sURL }).done(function( data ) {
        alert(data);
        console.log(data);
        document.write(data);
    });    
}

PHP:myproxy.PHP

<?php
    header('Content-Type: application/xml; charset=utf-8');
    $file = file_get_contents($_POST['requrl']);
    echo $file;
?>

请注意,如果您计划将其用于其他类型的内容,则需要更改/删除标题行。

您的浏览器允许您从其他网站AJAX XML

如果是这种情况,那么您需要更换或更新您的web浏览器。。

上述解决方案并不复杂

这实际上是复制和粘贴代码的准备工作,JS函数将以三种最常见的方式返回结果/数据/内容。。。

PHP脚本也是复制粘贴的。。所以,如果你安装了PHP。

您所需要做的就是在与html文档相同的位置创建一个新的文本文件,并将其命名为"myproxy.php",这个例子就可以了。

这是一个合适的XmlHttpRequest,带有回调函数来处理XML:

<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
function loadXMLDoc(url){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                callbackFunction(xmlhttp.responseText);
            }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
function callbackFunction(response){
    if (window.DOMParser){  //  Non IE
        var parser = new DOMParser();
        var xml_doc = parser.parseFromString(response,"text/xml");
    }else{                  // Internet Explorer
        var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
            xml_doc.async = false;
            xml_doc.loadXML(response);
    }
    //  Do something with your 'xml_doc' object variable here
    console.log(xml_doc)    //  Debugging only.. to see the XML in the browser console for your own reference
    var x = xml_doc.getElementsByTagName("title")[0]
    var y = x.childNodes[0];
    document.write(y.nodeValue);
}
// Call the function to begin code execution
loadXMLDoc('http://www.w3schools.com/dom/books.xml')
</script>
</body>
</html>

这是工作代码,所以你可以删除你所拥有的,并将其直接放在它的位置。祝你好运!

如果您计划在自己的服务器上托管文件以通过XHR访问,我提供的代码就是用于此目的的。如果w3schools.com在您请求的XML文件上有一个"Access Control Allow Origin:*"头,它也可以工作。但他们没有。因此,您需要将XML文件放在浏览器安全性允许您访问它的位置(与网页的域名相同)。否则,您的浏览器将继续阻止资源,并在控制台中出现"跨源请求被阻止"错误。