在javascript中从localhost访问XML文件 [英] Accessing XML file from localhost in javascript

查看:143
本文介绍了在javascript中从localhost访问XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript阅读XML文件,然后将其显示在html页面中

I am reading XML file using javascript then display it in my html page

它在FireFox上运行良好.

it is working perfectly on FireFox.

我用Google搜索后发现,这是因为我的文件在硬盘上是本地文件,这就是为什么Chrome和IE无法正常工作,而Chrome给出此错误的原因

I googled and found that it is because my file is local in my harddisk that is why Chrome and IE do not work, and Chrome gives this error

clocks.html:20 Failed to load file:///B:/Data/clocks.xml: 
Cross origin requests are only supported for protocol schemes: 
http, data, chrome, chrome-extension, https.

所以我创建了一个本地网站并在其中添加了文件

so I created a local website and added the file there

http://localhost/clocks.xml

我可以通过该链接访问文件,但是当我在脚本中将clocks.xml替换为以http://localhost/clocks.xml结尾的页面时,即使在FireFox中也无法正常工作,并且无法从FireFox获取此错误

I can access the file through that link, but when I replace clocks.xml in my script with http://localhost/clocks.xml ended with page is not working not even in FireFox and getting this error from FireFox

Cross-Origin Request Blocked: The Same Origin Policy disallows 
reading the remote resource at http://localhost/clocks.xml. 
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing).[Learn More]

如何使它在所有浏览器中都能正常工作

how can I get this working in all browsers

我的脚本在这里

        window.onload = function() {
            getClockInformation();
        }

        function getClockInformation() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    updateClocks(this);
                }
            };
            xhttp.open("GET", "http://localhost/clocks.xml", true);
            xhttp.send();
        }

推荐答案

如果您只想直接从磁盘运行它,则将无法使用ajax作为Chrome,并且可能其他浏览器也不允许加载file:///网址.

If you want to just run it directly from disk you won't be able to use ajax as Chrome and probably other browsers just wont allow loading of file:/// urls.

您可以通过使用文件输入或拖放操作来获取文件来解决此问题

You can get around this by using a file input or drag and drop operation to get the file

HTML

Select the clocks.xml file
<input type="file" id="xmlfile">

JavaScript

Javascript

var fileInput = document.querySelector("#xmlfile");
fileInput.addEventListener("change",function(e){
  var file = fileInput.files[0];
  var reader = new FileReader();
  reader.onloadend = function(){
    var data = reader.result;
    //data will now contain the xml text
    //use DOMParser to parse it
    var xmlDocument = (new DOMParser()).parseFromString(data,"application/xml");
    //then use the various element methods to get the elements you need
    //for instance if you had a <clock> element
    var clockData = xmlDocument.querySelector("clock").textContent
  };
  reader.readAsText(file);
})

否则,您将需要设置cors或从服务器中加载html和xml.

Otherwise you would need to setup cors or load both the html and xml from your server.

DOMParser api

FileReader api

这篇关于在javascript中从localhost访问XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆