admin管理员组

文章数量:1355535

I am trying to read a file (stored on the web server) into an array. When I print the array I currently get "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!

I am trying to read a file (stored on the web server) into an array. When I print the array I currently get "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!

Share Improve this question asked Feb 19, 2013 at 23:42 spogebob92spogebob92 1,4844 gold badges23 silver badges33 bronze badges 1
  • 1 var array is local variable for the success callback and cardRules wasn't filled in you code – krasu Commented Feb 19, 2013 at 23:45
Add a ment  | 

3 Answers 3

Reset to default 3

The 'cardRules' variable never gets populated with the array data. Instead of var array = data.split('\n'); just use cardRules = data.split('\n');

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

Try full url instead of just relative linking

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

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

本文标签: Reading txt file into array JavascriptjQueryStack Overflow