0

Here's what I wanted to do. 1. Load a text file (server side) say testJSON.txt [DONE] 2. Load the content from text file [DONE] 3. Dump the loaded data into an array for JavaScript [??HOW??]

Code snippet :

var data2, externalData;
$.get("testJSON.txt",function(externalData){
// Pop an alert showing the typeof the object taken from text file [WORKS]
alert("Data from JavaScript file: " + externalData + "typeof object " + typeof externalData);
// Convert the string from text file into JavaScript array [WORKS]
data2 = eval('['+ externalData + ']');
});
// **DOESN'T WORK**
alert(data2);
user42826
  • 101
  • 1
  • 8
  • 1
    `$.get` is async.. you're trying to access `data2` while the call is in progress.. you need to use another callback or do your work in the provided callback from `get` – tymeJV Feb 13 '15 at 17:29
  • What are you using on the server side? – ps2goat Feb 13 '15 at 17:29
  • I just have a text file, plain JavaScript and HTML. – user42826 Feb 13 '15 at 17:30
  • @tymeJV, how do I do it? – user42826 Feb 13 '15 at 17:46
  • I didn't mark this as a duplicate-- I marked it as not clear what OP was asking. You can't load stuff from the file system into javascript on the server if you don't have code on the server side. – ps2goat Feb 13 '15 at 17:57
  • @ps2goat, The code is on the server side, actually everything is on the client side, I am merely loading the web page. Is there a solution to this? So far not really luck to have a solution yet. – user42826 Feb 13 '15 at 18:07

0 Answers0