Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a local file using jQuery? (with file://)

Tags:

jquery

Is there any way to load data from a data file (eg a JSON .js file) using jQuery?

eg:

$.get("file:///C:/objectData.js", function() { alert('Load was performed.'); });

At the moment, instead of doing a simple HTTP GET, JQuery appears to be trying to do an OPTIONS request on it, which fails on a file:// URI. I just want to load the data in so that the site can be used in an offline environment (without a web server installed).

like image 266
NickG Avatar asked Jan 24 '13 17:01

NickG


1 Answers

GET requires an HTTP connection, so without a local web servber it won't work. While you can open and read a file using HTML5, you can't load a JavaScript resource that way.

If the page is loaded locally, you'd usually load the JS using a script tag.

<script type='text/javascript' src='/objectData.js'></script>

The only way around this may be in this answer: Is it possible to load in a local version of a JavaScript file instead of the server version?

or this (both require making a local pseudo-server) :

like image 125
Diodeus - James MacFarlane Avatar answered Oct 21 '22 13:10

Diodeus - James MacFarlane