JQuery.getJSON () reading a local file

How do you read the file in the current directory using jQuery.getJSON ()?

I am trying to do something simple (with my data.json file in the same directory as my html file):

$.getJSON("./data.json")

and I get the error:

XMLHttpRequest cannot load file: /// C: /Projects/test/data.json. The origin of null is not allowed through Access-Control-Allow-Origin.

I tried all sorts of path combinations, but it doesn't seem to work ...

EDIT: I use Chrome, but I would like to work in all browsers ...

+5
source share
4 answers

If you use Google Chrome, he intends that AJAX on the way file:///never works.

crbug / 40787

+9
source

, . , . , . () LAMP, XAMPP. AJAX.

+2

URL, jQuery json jsonp

. .

+1

An alternative to installing a local LAMP server is to use a simple python HTTP server. As long as you have python installed, just open bash and use the python interpreter.

For python 2 use:

python -m SimpleHTTPServer 8000

For python 3 use:

python -m http.server 8000

Then just point your browser to localhost: 8000 and you should have no problem accessing the file if it is in the same directory.

0
source

All Articles