javascript read a text file

Posted by Cyprus106 on Stack Overflow See other posts from Stack Overflow or by Cyprus106
Published on 2010-05-17T19:09:55Z Indexed on 2010/05/17 19:20 UTC
Read the original article Hit count: 236

Filed under:
|
|

I have looked everywhere and surprisingly can't find a good solution to this! I've got the following code that is supposed to read a text file and display it's contents. But it's not reading, for some reason. Am I doing something wrong?

FTR, I can't use PHP for this. It's gotta be Javascript.

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://www.mysite.com/todaysTrivia.txt", true);
txtFile.send(null);
txtFile.onreadystatechange = function() {   
    if (txtFile.readyState == 4) {  // Makes sure the document is ready to parse.
        alert(txtFile.responseText+" - "+txtFile.status);       
        //if (txtFile.status === 200) {  // Makes sure it's found the file.         
            var doc = document.getElementById("Trivia-Widget");
            if (doc) {                  
                doc.innerHTML = txtFile.responseText ;   
            }
        //}
    }
    txtFile.send(null);
}

Any good ideas what I'm doing wrong? It just keeps givimg me a zero status.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about text