Reading .txt file into Javascript / jQuery array

I am trying to read a file (stored on a web server) into an array. When I print the array, I am currently getting "undefined". Here is the code im using:

var cardRules = new Array;
    $.get('UserFile.txt', function(data){
            var array = data.split('\n');
            console.log(cardRules);
        });

      

Any help would be appreciated!

+3


source to share


3 answers


The 'cardRules' variable is never populated with array data. Instead,    var array = data.split('\n');

just use   cardRules = data.split('\n');



+2


source


var cardRules = new Array();
    $.get('UserFile.txt', function(data){
            cardRules = data.split('\n');
            console.log(cardRules);
        });

      



0


source


Try using full url instead of relative anchor

http://yoursite.com/yourfilelocation/yourfile.txt

$.get('http://yoursite.com/yourfilelocation/yourfile.txt', function(data){ var cardRules = data.split('\n'); console.log(cardRules); });

0


source







All Articles