JavaScript XMLHttpRequest 语法错误

发布于 2024-11-16 19:30:01 字数 756 浏览 2 评论 0 原文

我是 javascript 新手,并且有一些数据存储在我想在网页上使用的 txt 文件中。

但是,当我发送 XmlHttpRequest 来获取文本文件时,firefox 在我尝试读入的 .txt 的第一行抛出语法错误。

这是我的代码:

    var txtFile = new XMLHttpRequest();

    txtFile.onreadystatechange = function() {

        if (txtFile.readyState === 4) {  

                if (txtFile.status === 200) {               
                    allText = txtFile.responseText;
                    lines = txtFile.responseText.split("\n"); 
            }
        }
    }
    txtFile.open("GET", "File:\\\myinfo.txt", true);    
    txtFile.send(null); 

这是来自 firefox 的错误消息的内容:

语法错误: "File:\\myinfo.txt" Line:1

然后这里有该行上的文本

我认为这可能意味着我不允许访问本地文件,这是 Firefox 让我知道这一点的方式。

有谁有此错误的经验或知道它的含义吗?

I am new to javascript and have some data stored in a txt file that I want to use on my webpage.

However when I send an XmlHttpRequest to get the text file firefox throws a syntax error on the first line of the .txt I am trying to read in.

Here is my code:

    var txtFile = new XMLHttpRequest();

    txtFile.onreadystatechange = function() {

        if (txtFile.readyState === 4) {  

                if (txtFile.status === 200) {               
                    allText = txtFile.responseText;
                    lines = txtFile.responseText.split("\n"); 
            }
        }
    }
    txtFile.open("GET", "File:\\\myinfo.txt", true);    
    txtFile.send(null); 

Here is what the error message from firefox reads:

syntax error:
"File:\\myinfo.txt" Line:1

then down here it has the text that is on this line

I think it may mean that I'm not allowed to access a local file and this is firefox's way of letting me know this.

Does anyone have any experience with this error or know what it means?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-11-23 19:30:01

反斜杠字符在字符串中具有特殊含义。它表示下一个字符在某种程度上是特殊的,例如,您可以通过使用反斜杠转义它来在字符串中包含引号:

'It\'s a string'

为了包含文字反斜杠,您将其放置两次:

'This is one backslash \\ character'

因此,在您的示例中,您有 \\\m。前两个斜杠变成一个斜杠,并且 \m 未被识别为有效的转义序列,因此您会收到错误。

将您的 URL 更改为使用正斜杠(它没有这种特殊含义,并且无论如何都是在 URL 中使用的正确斜杠类型),或者双反斜杠:

"file:///myinfo.txt"
"file:\\\\\\myinfo.txt"

另外,请注意,此 URL 实际上并不指向对于任何东西,它应该是这样的:

"file:///C:/myinfo.txt"

另外,如上所述,XMLHttpRequest 仅适用于与其所在页面托管的同一域,因此如果您的页面位于 http://www.example.com/ 您只能访问 http: //www.example.com/

The backslash character has special meaning in a string. It signifies that the next character is special in some way, for example you can include a quote inside a string by using the backslash to escape it:

'It\'s a string'

In order to include a literal backslash, you put it twice:

'This is one backslash \\ character'

So, in your example, you have \\\m. The first two slashes become one slash, and the \m is unrecognised as a valid escape sequence and so you get the error.

Change your URL to either use forward slashes (which don't have this special meaning and is the correct type of slash to use in a URL anyway), or double-up your backslashes:

"file:///myinfo.txt"
"file:\\\\\\myinfo.txt"

Also, note that this URL doesn't actually point to anything, it should be something like this:

"file:///C:/myinfo.txt"

Also, as pointed out above, XMLHttpRequest only works against the same domain as the page it's on is hosted, so if your page is on http://www.example.com/ you can only access resources on http://www.example.com/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文