3

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!

1
  • 1
    var array is local variable for the success callback and cardRules wasn't filled in you code Commented Feb 19, 2013 at 23:45

3 Answers 3

3

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

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

Comments

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

8 Comments

It still is saying "undefined"
\n - represent new line.Why you split the array by \n ?
because I want to split the array for each new line in the txt file
I don't think that an array has new line between each value. Can you show me your array?
I'm not sure I'm explaining myself clear enough. For each new line in my UserFile.txt, I want it to be stored in the index of an array, and then the next index etc.
|
0

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); });

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.