OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get data from external file [closed]

  • Thread starter Thread starter The-IT
  • Start date Start date
T

The-IT

Guest
I'm working in vanilla JS (I might convert the code to JQuery, but we'll see), and I'd like to have a file in the same folder as the JS file (this file would be publicly accessible) simply containing a bunch of data for the JS script to work with. I'd rather not have all the data hard-coded into the script for a number of reasons.

The data can be loaded immediately at the beginning of run-time and isn't dynamic at all. I was hoping I could pass it in as a dependency, but that doesn't seem to work.

Right now, as a compromise, I hard coded it into a different script that sets a variable and loads that script before the main script, which then accesses that variable as a global (you can probably understand why I hate the state it's currently in, lol).

Using fetch doesn't work locally due to the CORS issue, and I'd really rather not try and get it via an HTTP request due to needing to hard-code the URI. If the data file is being served anyway, I really don't see why this needs to be so difficult.

enter image description here
<p>I'm working in vanilla JS (I might convert the code to JQuery, but we'll see), and I'd like to have a file in the same folder as the JS file (this file would be publicly accessible) simply containing a bunch of data for the JS script to work with. I'd rather not have all the data hard-coded into the script for a number of reasons.</p>
<p>The data can be loaded immediately at the beginning of run-time and isn't dynamic at all. I was hoping I could pass it in as a dependency, but that doesn't seem to work.</p>
<p>Right now, as a compromise, I hard coded it into a different script that sets a variable and loads that script before the main script, which then accesses that variable as a global (you can probably understand why I hate the state it's currently in, lol).</p>
<p>Using <code>fetch</code> doesn't work locally due to the CORS issue, and I'd really rather not try and get it via an HTTP request due to needing to hard-code the URI. If the data file is being served anyway, I really don't see why this needs to be so difficult.</p>
<p><a href="https://i.sstatic.net/AjawY38J.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AjawY38J.png" alt="enter image description here" /></a></p>
Continue reading...
 
Top