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 :(

3

1 Answer 1

1

I made the call asynchronous.

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

Now it is working

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

Comments

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.