Import Text File Using Javascript

html, import, javascript, text

Solution

Use AJAX. I suggest encoding the file in JSON format, rather than plain text. If you use jQuery, you can then use `$.getJSON('filename.txt')` to read the file and parse it into a Javascript object in one operation.

Problem

I am currently working on an application that handles a fairly large amount of data. Currently, I've hard-coded those values into the Javascript iself (defining global arrays), but this method does not seem sustainable. Is there a way to use Javascript to parse a .txt file located in the same directory on the server? I know this question has probably been asked before, but I've only found answers pertaining to accessing system-local text files.

Original source