JavaScript的:阅读使用AJAX文本文件文本文件、JavaScript、AJAX

2023-09-10 13:54:41 作者:狂霸炫酷吊炸天ゝ

不能让AJAX工作!我有一个网站,一个大帐篷,得到它的工作!但我想它找到一个文本文件字幕的文字,我想它读取文本文件中的文本(这是一条线),并将其分配给名为含量,这是在脚本标记的全局变量。

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.

在我所经营的网站(本地IIS),字幕文字是:未定义(不带引号)

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

为什么不将其分配文本变量内容

Why isn't it assigning the text to the variable 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();
    }

编辑:一百万个感谢@kuncajs,他指出,我忘了打电话的功能! :) 固定!感谢所有的人!

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

推荐答案

不要使用类似本地路径: C:\ Users \用户票价\ Dropbox的\同步\学院\计算\ DeltaOne \ MarqueeText.txt

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

将它放在你的IIS的www目录并说明喜欢的路径: 本地主机/的text.txt

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

服务器可以限制访问您的文件系统,你也应该尝试像的text.txt 相对路径或绝对路径 /text.txt 这样的路径,当你将其部署在生产环境中工作,甚至。

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

编辑: 因此,如果这没有帮助,那么要确保你真的叫 loadXMLDoc()把功能。另外,请检查你做的一切都是的在的AJAX的结束!我的意思是你做任务的,如果 - !当AJAX做,但你也应该初始化选取框后!文字是加载。如果你这样做之前,它是不确定的。

So if this did not help then make sure that you really call the loadXMLDoc() function. Also check that everything you do is after the 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