Javascript:使用 AJAX 读取文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15547407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:09:41  来源:igfitidea点击:

Javascript: Read Text File using AJAX

javascripthtmlajaxmarquee

提问by Fares K. A.

Can't get AJAX to work! I have a marquee on a website, got it working! But I want it to find the text of the marquee in a text file, and I want it to read the text in the text file (which is one line) and assign it to the variable called content, which is a global variable in the script tag.

无法让 AJAX 工作!我在一个网站上有一个选框,让它工作了!但我希望它在文本文件中找到选取框的文本,并且我希望它读取文本文件中的文本(这是一行)并将其分配给名为 的变量content,该变量是脚本中的全局变量标签。

When I run the website (local IIS), the marquee text is: "undefined" (without the quotes).

当我运行网站(本地 IIS)时,选取框文本是:“未定义”(不带引号)。

Why isn't it assigning the text to the variable content?

为什么不将文本分配给变量content

    var content

    function loadXMLDoc()
    {   
        var textfile;
        if (window.XMLHttpRequest)
        { 
            textfile = new XMLHttpRequest(); 
        }
        textfile.onreadystatechange = function ()
        {   
            if (textfile.readyState == 4 && textfile.status == 200)
            { 
                content = textfile.responseText; 
            }
        }
        textfile.open("GET", "C:\Users\Fares\Dropbox\Sync\College\Computing\DeltaOne\MarqueeText.txt", true);
        textfile.send();
    }

EDIT:A million thanks to @kuncajs, as he pointed out I forgot to call the function! :) Fixed! Thanks to everyone else!

编辑:一百万感谢@kuncajs,正如他指出的那样,我忘记调用该函数!:) 固定的!感谢其他所有人!

采纳答案by kuncajs

Do not use local paths like: C:\Users\Fares\Dropbox\Sync\College\Computing\DeltaOne\MarqueeText.txt

不要使用本地路径,例如: C:\Users\Fares\Dropbox\Sync\College\Computing\DeltaOne\MarqueeText.txt

Place it in the www directory of your IIS and state the path like: localhost/text.txt

把它放在你的 IIS 的 www 目录中,并声明如下路径: localhost/text.txt

The server can have restricted access to your filesystem and also you should try relative paths like text.txtor absolute paths /text.txtso the paths work even when you deploy it in the production environment.

服务器可以限制访问您的文件系统,并且您应该尝试使用相对路径text.txt或绝对路径,/text.txt这样即使您将其部署在生产环境中,这些路径也能正常工作。

EDIT:So if this did not help then make sure that you really call the loadXMLDoc()function. Also check that everything you do is afterthe AJAX ends! I mean you do the assignment in the if - when AJAX is done but you should also initialize your marquee !AFTER! the text is loaded. If you do it before it will be undefined

编辑:因此,如果这没有帮助,请确保您确实调用了该loadXMLDoc()函数。另外,请检查你做的一切都是经过了AJAX结束!我的意思是你在 if 中进行分配 - 当 AJAX 完成但你也应该初始化你的选取框!AFTER!文本已加载。如果你之前这样做,它将是未定义的

回答by TheC4keIsASpy

Try using a relative or absolute path first.

首先尝试使用相对或绝对路径。

If that doesn't work check that when using your browser you can access the file (let's say your website is on mysite.com/index.html, try opening mysite.com/text.txt)

如果这不起作用,请检查在使用浏览器时是否可以访问该文件(假设您的网站已打开mysite.com/index.html,请尝试打开mysite.com/text.txt

If you can't access it using your browser then you will have to configure your server to allow this file to be read, never tried IIS so I can't help you there.

如果您无法使用浏览器访问它,那么您必须配置您的服务器以允许读取此文件,从未尝试过 IIS 所以我无法帮助您。

Also since you are asking your XHR to be asynchronous it might take a little time before the variable is populated (depending on your/your server's speed)

此外,由于您要求 XHR 是异步的,因此在填充变量之前可能需要一些时间(取决于您/您的服务器的速度)