0

i'm trying to read in a local file in javascript, I am not publishing this online so I only need it to work with my broweser (Firefox), i am currently trying to parse it using an XML Http request:

var txtFile = new XMLHttpRequest();
        txtFile.open("GET", "file://Users/spe_eddy_gonzalez/Dropbox/Me/Hon Proj/Wikipedia/simplewikitext.txt", true);
        txtFile.onreadystatechange = function() {
    if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
            if (txtFile.status === 200) {  // Makes sure it's found the file.
            allText = txtFile.responseText;
            lines = txtFile.responseText.split("\r\n"); // Will separate each line into an array
        } //"\r\n" 
    }
}
console.log($(txtFile).val());

var stringT = (new XMLSerializer()).serializeToString(txtFile);

but get the following error:

[11:22:43.970] NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIDOMSerializer.serializeToString] @ http://127.0.0.1:8020/CharCount2/character_counter2.js:26\

any help would be very welcome. I'm thinking about re-writing my whole system in Python as i'm struggling with the limited I/O functionality of Javascript, and i have much more experience in Python

1
  • Does it get as far as the console.log? If so, what did it log? Commented Mar 2, 2013 at 11:42

1 Answer 1

3

Ajax does not work with the file:// protocol. You should use a simple webserver on your machine such as Apache or Nginx.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, the console.log line won't work as the Ajax call is asynchronous. Same for the XMLSerializer, which isn't needed anyway as you get simple text and not XML. If you're going to use jQuery, I suggest using it's $.get function to do the XMLHttpRequest behind the scenes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.