javascript - 如何在json中给出本地文件的url路径

标签 javascript json html

我正在尝试使用 json 显示存储在本地磁盘上的 txt 文件 myTutorials.txt 的内容,但它不显示任何内容。我只想知道 url "G:/WebTechnologies/myTutorials.txt";

的正确格式

我的网页代码如下:

<!DOCTYPE html>
<html>
<body>

<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "G:/WebTechnologies/myTutorials.txt";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i < arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' + 
        arr[i].display + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>

最佳答案

你必须在前面添加 file://localhost

示例:file://localhost/G:/WebTechnologies/myTutorials.txt

出于安全原因,您可能不得不这样做 loading text file through Ajax

关于javascript - 如何在json中给出本地文件的url路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27396611/

相关文章:

javascript - JSDoc + IDE 与 TypeScript

javascript - 如何在 JSON 中找到最高键?

javascript - 人工制品出现在网站 : CSS issue

html - 将 body floater 的最小高度调整为包含多个 div 的导航 floorer

javascript - 确定随机变量在javascript中是否连续

javascript - 以 react 形式将 firebase.firestore.timestamp 转换为数据的 patchValue 之前的日期

json - Mongodb更新语句: How do i update the json object?

html - 我的下拉菜单没有下来

javascript - 如何使用 Dojo 重现此 JQuery ajax

Python - Pandas - 如何在数据帧合并后从 to_json 中删除空值