0

I'm not sure why this is happening. The below function is executing correctly. The path file is correct. I've tried to add "text" after, and json etc. But the data is empty??? I'm at my wits end, I'm not sure why this happening. Please help

var str = "";
$.get(
   "steps.txt",
    function(data){
        str = data;
    }, 
    "text"
);
Viz
  • 11
  • 3
  • How are you determining that the data is empty? – Taplar Apr 13 '20 at 14:43
  • I am doing console.log(str) but it gives nothing. The file definitely has data in it, but I'm ripping my hair out over this. I can't seem to see why it's wrong. – Viz Apr 13 '20 at 14:47
  • Where are you doing the console.log at? Remember that ajax requests are asynchronous. – Taplar Apr 13 '20 at 14:47
  • just after the call – Viz Apr 13 '20 at 14:48
  • Ajax is asynchronous. It will not have finished, or probably even started, by the time your console log immediately after it is invoked. You can stick your console.log inside the ajax callback method and log it there. – Taplar Apr 13 '20 at 14:49
  • wow, that worked, but then how do I get the data out and use it in the rest of my file. Because it's not working outside. – Viz Apr 13 '20 at 14:55
  • Read the duplicate. – Taplar Apr 13 '20 at 14:56
  • sorry, I'm a bit new to this. How do I do that? – Viz Apr 13 '20 at 14:58

0 Answers0