【问题标题】:Load XML with script使用脚本加载 XML
【发布时间】:2015-07-23 17:43:44
【问题描述】:

我正在尝试使用此脚本加载 XML 文件,但是当我运行代码并按下“按钮”时,我收到以下消息:

阻止来自其他来源的请求:同源策略阻止 远程资源读取https://therouteofmyxml.xml(原因: 缺少 CORS ' Allow- Access- Control- header Origin ')。

是因为我的浏览器上有任何扩展程序或插件吗?谢谢!!

<script>
        function loadXMLDoc(){
            xmlhttp=null;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            if (xmlhttp!=null){
                xmlhttp.onreadystatechange=function(){
                    if (xmlhttp.readyState==4 && xmlhttp.status==200){
                        xmlDoc=xmlhttp.responseXML;
                        var txt="";
                        x=xmlDoc.getElementsByTagName("offer");
                        for (i=0;i<x.length;i++){
                            txt=txt + x[i].childNodes[0].nodeValue + "<br>";
                        }
                        document.getElementById("myDiv").innerHTML=txt;
                    }
                }
                xmlhttp.open("GET","https://therouteofmy.xml",true);
                xmlhttp.send();
            }
            else{
                alert("Your browser does not support XMLHTTP.");
            }
        }
    </script>
</head>
<body>

<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Mis Ofertas</button>

【问题讨论】:

  • 不,它与扩展或插件无关。出于安全原因,您通常不应该从另一个域加载 XML。对同源政策进行一些研究以获取更多信息。

标签: html xml xslt


【解决方案1】:

如果您的带有脚本的 HTML 是从 http://example.com/ 加载的,则它只能从同一来源加载 XML,例如一个文件http://example.com/file.xml,但不是来自像http://example.org/这样的不同来源,除非其他站点合作并明确允许此类请求。有关详细信息,请参阅https://de.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2016-04-25
    • 2019-06-29
    相关资源
    最近更新 更多