简体   繁体   English

如何使用 XMLHttpRequest 从文件系统读取文件?

[英]How can I use XMLHttpRequest to read a file from the filesystem?

In this code I am trying to open a file (skills.xml) which is in the same folder as the current page, but it's returning null.在此代码中,我试图打开与当前页面位于同一文件夹中的文件 (skills.xml),但它返回 null。

What am I doing wrong?我究竟做错了什么?

function loadXMLDoc() {

    var xmlhttp = new XMLHttpRequest();
    alert(xmlhttp.readyState + "       " + xmlhttp.status);

    xmlhttp.open("GET", "skills.xml", true);

    xmlhttp.onreadystatechange = function() {
        alert(xmlhttp.readyState + "       " + xmlhttp.status);
        if (xmlhttp.readyState == 4) {
            myFunction(xmlhttp); 
        }
    }

    xmlhttp.send();

}

It didn't work for me for a long time, and the problem is that i didn't have a server, i was just visualizing the file in the file system.它对我来说很长时间没有用,问题是我没有服务器,我只是在文件系统中可视化文件。 If you are using VSCode there is a simple work-around to this: you can install the Live Server extension and run the html file on the server using the button in the bar at the bottom that says "Go Live".如果您使用的是 VSCode,有一个简单的解决方法:您可以安装Live Server扩展并使用底部栏中显示“上线”的按钮在服务器上运行 html 文件。 Note : you might need to reload the IDE.注意:您可能需要重新加载 IDE。

(PS: i know it's been long since this question was asked but feel free to tell me if you found another way to deal with the problem or if there were other problems) (PS:我知道这个问题已经很久没有提出来了,但是如果你找到了另一种方法来解决这个问题或者是否还有其他问题,请随时告诉我)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM