Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery.getJSON() reading a local file

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

I'm trying 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. Origin null is not allowed by Access-Control-Allow-Origin.

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

EDIT: I'm using Chrome, but I'd like to work in all browsers...

like image 775
user1167650 Avatar asked Jul 02 '12 16:07

user1167650


People also ask

How jQuery read data from JSON file?

Projects In JavaScript & JQuery To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

What is jQuery getJSON?

jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

Is getJSON an Ajax call?

getJSON()) is an AJAX method that is used to fetch JSON data using HTTP GET request.

What is the difference between getJSON and Ajax in jQuery?

getJSON() is equal to $. ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error. So you were mostly right about the two being pretty much the same :).


1 Answers

If you are using Google Chrome, it is intentional that AJAX on file:/// paths never works.

crbug/40787

like image 129
ephemient Avatar answered Oct 21 '22 04:10

ephemient