1

I made a Javascript function for reading csv files like this :

    function readTextFile(file) {
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function () {
        if(rawFile.readyState === 4) {
            if(rawFile.status === 200 || rawFile.status == 0) {
                alert(rawFile.responseText);
                return rawFile.responseText;
            }
        }
    }
    rawFile.send(null);
}

The alert inside the function returns exactly what's in the file, but as soon as I use the function in my HTML like this :

<script src='readCsvFile.js'></script>
<script>
    alert(readTextFile('../users.csv'));            
</script>

The funtion seems to be returning undefined. At least that's what the alert/ consol.log shows.

  • Think carefully, what is the code which receives the returned value from the event handler, i.e. where exactly are you waiting a value from it? – Teemu Jan 27 '18 at 09:13

0 Answers0