Mike D
Mike D

Reputation: 2841

reading file with xmlhttprequest results in error console entry

I used the following code to read a file from javascript

    var filePath = "SBL_PROBES.txt";
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET",filePath,false);
    xmlhttp.send(null); 
    var fileContent = xmlhttp.responseText;
    var fileArray = fileContent.split('\n')
    var n = fileArray.length;

Things go fine and I can access the file contents from fileArray. BUT an error appears on the firefox error console complaining about the contents of line 1 of the file. I don't want the file interpreted by javascript or firefox, all I want are the contents which I parse with javascript.

The firefox console says

Error: syntax error Source File: file:///C:/Documents%20and%20Settings/Mike/Desktop/mustache/SBL_PROBES.txt Line: 1, Column: 1 Source Code: "title" "Short Name" "Long Name" "Current","Maximum","Minimum","Day Max","Day Min"

If I put <blockquote> </blockquote> in the file, the errors go away!

What's going on and how can I fix it?

Do I need to do anything to close the file? Will things be cleaned up when these vars go out of scope?

Upvotes: 5

Views: 6151

Answers (1)

Saxoier
Saxoier

Reputation: 1287

This should fix it:

xmlhttp.overrideMimeType('text/plain');

By default it seems that local files are parsed with the XMLParser.

Upvotes: 13

Related Questions