最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript: Read Text File using AJAX - Stack Overflow

programmeradmin1浏览0评论

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.

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();
    }

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

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.

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();
    }

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

Share Improve this question edited Mar 21, 2013 at 12:58 Fares K. A. asked Mar 21, 2013 at 12:22 Fares K. A.Fares K. A. 1,2855 gold badges24 silver badges49 bronze badges 1
  • Thanks @BLSully for the formatting – Fares K. A. Commented Mar 21, 2013 at 12:34
Add a ment  | 

2 Answers 2

Reset to default 2

Do not use local paths like: 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

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.

EDIT: 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

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./index.html, try opening mysite./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.

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)

发布评论

评论列表(0)

  1. 暂无评论