Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 510
Mike D Avatar asked Sep 10 '11 21:09

Mike D


1 Answers

This should fix it:

xmlhttp.overrideMimeType('text/plain');

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

like image 67
Saxoier Avatar answered Nov 14 '22 21:11

Saxoier