求大佬帮忙,JS实现谷歌浏览器读取xml文件的值

qq_41334210 2021-08-09 17:20:54

编译的是报跨域的错,应该怎样才能读取到PC549,麻烦大佬指点

index.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <WriteTime>2016-10-09 14:37:43</WriteTime>
  <Status>1</Status>
  <log>
  </log>
  <ComputeName>PC549</ComputeName>
</root>

 

JS我是这样写的

<body>

  <input id="test" type="button" value="按钮">

</body>

<script>

  $("#test").click(function () {

        var xmldoc = loadXmlFile("./index.xml");        

        var elements = xmldoc.getElementsByTagName("ComputeName");

        alert(elements[0].firstChild.nodeValue);

    });

    function loadXmlFile(xmlFile) {

        var xmlDom = null;

        if (window.ActiveXObject) {

            xmlDom = new ActiveXObject("Microsoft.XMLDOM");

            xmlDom.async = "false";

            xmlDom.load(xmlFile);

            console.log(1);

        }

        else if (document.implementation && document.implementation.createDocument) {

            var xmlhttp = new window.XMLHttpRequest();

            xmlhttp.open("GET", xmlFile, false);

            xmlhttp.send(null);

            xmlDom = xmlhttp.responseXML;

            console.log(2);

        }

        else {

            xmlDom = null;

            console.log(3);

        }

        return xmlDom;

    };

</script>

...全文
857 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSDN-Ada助手 2023-01-13
  • 打赏
  • 举报
回复
您可以前往 CSDN问答-编程语言 发布问题, 以便更快地解决您的疑问

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧