Javascript 访问特定URL以在JS中读取文件

Javascript 访问特定URL以在JS中读取文件,javascript,file,Javascript,File,我正在尝试用Java脚本读取一个文件。我使用的是XAMPP服务器,所有文件都在htdocs文件夹中。但当我试图从其他目录读取文件时,它不起作用 错误: NS_ERROR_DOM_BAD_URI: Access to restricted URI denied request.send(null); JavaScript: <!DOCTYPE html> <html> <body> <textarea id="box" rows="4" cols

我正在尝试用Java脚本读取一个文件。我使用的是XAMPP服务器,所有文件都在htdocs文件夹中。但当我试图从其他目录读取文件时,它不起作用

错误:

NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
request.send(null);
JavaScript:

<!DOCTYPE html>
<html>
<body>
    <textarea id="box" rows="4" cols="50"> </textarea>


<script>

//get the contents of the text file START   
//Make sure in the JsonData.txt file, after each line there should not be any space ie when you click RIGHT arrow at the end of each line the cursor should go to next line beginning.


            function FileHelper()
            {}
            {
                FileHelper.readStringFromFileAtPath = function(pathOfFileToReadFrom)
                {
                    var request = new XMLHttpRequest();
                    request.open("GET", pathOfFileToReadFrom, false);
                    request.send(null);
                    var returnValue = request.responseText;

                    return returnValue;
                }
            }

            var pathOfFileToRead = "file://d:/sampleData.txt";

            var contentsOfFileAsString = FileHelper.readStringFromFileAtPath
            (
                pathOfFileToRead
            );

            var contents_value = contentsOfFileAsString;
            alert(contents_value);
            document.getElementById("box").value = contents_value;

            //document.getElementById("demo").innerHTML = contents_value;       

            //get the contents of the text file STOP
</script>

</body>
</html>

//从开始获取文本文件的内容
//确保在JsonData.txt文件中,每行之后不应有任何空格,即当您单击每行末尾的右箭头时,光标应转到下一行的开头。
函数FileHelper()
{}
{
FileHelper.readStringFromFileAtPath=函数(pathOfFileToReadFrom)
{
var request=new XMLHttpRequest();
打开(“获取”,pathoffieltoreadfrom,false);
请求发送(空);
var returnValue=request.responseText;
返回值;
}
}
var pathoffieltoread=“file://d:/sampleData.txt”;
var contentsOfFileAsString=FileHelper.readStringFromFileAtPath
(
病理学
);
var contents\u value=contentsOfFileAsString;
警报(内容值);
document.getElementById(“box”).value=contents\u value;
//document.getElementById(“demo”).innerHTML=contents\u value;
//获取文本文件STOP的内容

在JS'pathoffieltoread=filepath'中,如果我将文件保存在同一个htdocs目录中,它可以正常工作,但如果我像在上面的JS中一样提供本地目录路径,则无法正常工作。

您使用的是在浏览器中运行的javascript。不能使用file://协议读取文件,也不能使用驱动器号

你仍然可以做你想做的事。您需要使用url引用文件,并使用http://调用它。(你知道区别吗?!url有一个域名或ip地址指向一个web根目录,然后可能是一个端口号,然后用正斜杠分隔web根目录下的每个文件夹。windows路径有一个驱动器号,然后用反斜杠分隔每个文件夹。)


你的剧本中有很多小东西需要改进。我将首先移除第2行上的一对空大括号。然后,我认为没有人在同步模式下使用xmlhttp。您真的应该在回调中使用asynch,并在开展业务之前检查是否成功(200)

您正在使用浏览器中运行的javascript。不能使用file://协议读取文件,也不能使用驱动器号

你仍然可以做你想做的事。您需要使用url引用文件,并使用http://调用它。(你知道区别吗?!url有一个域名或ip地址指向一个web根目录,然后可能是一个端口号,然后用正斜杠分隔web根目录下的每个文件夹。windows路径有一个驱动器号,然后用反斜杠分隔每个文件夹。)

你的剧本中有很多小东西需要改进。我将首先移除第2行上的一对空大括号。然后,我认为没有人在同步模式下使用xmlhttp。您真的应该在回调中使用asynch,并在开展业务之前检查是否成功(200)