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...
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.
jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.
getJSON()) is an AJAX method that is used to fetch JSON data using HTTP GET request.
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 :).
If you are using Google Chrome, it is intentional that AJAX on file:///
paths never works.
crbug/40787
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With