0

I am trying to read a file while using google charts. It first read he data from a text file and then create chart using that data. I wrote this code in JS for this purpose

function readTextFile(file){
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false); // using synchronous call
    var allText;
    alert("Starting to read text");
    rawFile.onreadystatechange = function ()
    {   
        alert("I am here");
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                allText = rawFile.responseText;
            }
        }
    }
    rawFile.send(null);
    alert(allText);
    return allText;
}

The problem is : this method is getting called, but the control is not going in

rawFile.onreadystatechange = function ()
    {    ... }

Do anyone have any idea regarding this? Thanks in advance!

note: I am sending the file name in the parameter (file). I am not passing the address as both this HTML file and the text file are in the same folder.

update 1: I printed rawFile.readyState , and it always shows 1 which means server connection established. My code is a simple HTML code, not using any server for this purpose.

update 2: I tried adding file:/// before the file name that is not working too :(

Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68
  • 2
    Looks like a variation of the classic async question http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – elclanrs Jul 23 '15 at 07:30
  • Yes, it looks like, but I used rawFile.open("GET", file, false); "false" meaning it is synchronous. – Srijani Ghosh Jul 23 '15 at 08:00
  • 1
    check the link http://stackoverflow.com/questions/14446447/javascript-read-local-text-file – abhi312 Jul 23 '15 at 08:24

1 Answers1

1

I made the call asynchronous.

 rawFile.open("GET", file, true);

Now it is working

Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68