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!
-
1var array is local variable for the success callback and cardRules wasn't filled in you codekrasu– krasu2013年02月19日 23:45:00 +00:00Commented Feb 19, 2013 at 23:45
3 Answers 3
The 'cardRules' variable never gets populated with the array data. Instead of
var array = data.split('\n');
just use
cardRules = data.split('\n');
Comments
var cardRules = new Array();
$.get('UserFile.txt', function(data){
cardRules = data.split('\n');
console.log(cardRules);
});
8 Comments
Try full url instead of just relative linking
http://yoursite.com/yourfilelocation/yourfile.txt
$.get('http://yoursite.com/yourfilelocation/yourfile.txt', function(data){
var cardRules = data.split('\n');
console.log(cardRules);
});