1

I have a JSON file stored in the same folder as an HTML file for a webpage I'm working on. This HTML file has a script with a function that takes data in a variable in JSON form. I'm having trouble getting the contents of the JSON file into a variable in my HTML file to be used by the function. I am running the HTML file locally.

I've tried the following:

$(function(){
    $.getJSON("./data.json", function(jsdata) {
        json = jsdata;
        console.log(json);
    });
});

However, it only results in: XMLHttpRequest cannot load file. Is there any way to parse a local JSON file in Javascript?

The JSON was validated using http://jsonlint.com/

Using "data.json" gives the same error as before.

8
  • 2
    Try removing . in URL Commented Jun 4, 2015 at 14:20
  • It depends on your location of you Javascript file where you call this getJSON function. If the JS code is within the same level of the json file then indeed remove the . other wise ../ should do the trick Commented Jun 4, 2015 at 14:22
  • check in tool xrs request and check if file is json valid in validator Commented Jun 4, 2015 at 14:22
  • 1
    Does it make any difference that I am not hosting the file? I'm running the HTML locally. Commented Jun 4, 2015 at 14:27
  • 1
    It should be the problem Commented Jun 4, 2015 at 14:29

5 Answers 5

3

Do not use a .. If the json file is in the same folder as the location your js is running (i.e. your html file), it should be this:

$(function(){
    $.getJSON("data.json", function(jsdata) {
        json = jsdata;
        console.log(json);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

XMLHttpRequest cannot load file:///C:/Users/grooms/Desktop/SampleTimeline/data.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
You need to run your html from a website - not from the from a directory on the file system. For more info, see here: stackoverflow.com/questions/371875/…
Good to know. I get the noob of the day award.
2

You cannot load a local file using $.getJSON in jquery. You have to set a valid url of your file inside of your project, not a local file path because browser prevents it from loading for some security reason.

Comments

1

Dot in the url is causing the problem

Use this:

$.getJSON("/data.json", function(jsdata) { 
        json = jsdata;
        console.log(json);
    });

1 Comment

XMLHttpRequest cannot load file:///C:/data.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
1

The URL provided is wrong.

Use following:

$(function () {
    var baseUrl = 'http://....'; // Your base url of app

    // Use the absolute URL in here
    $.getJSON( baseUrl + "/data.json", function (jsdata) {
        json = jsdata;
        console.log(json);
    });
});

Comments

0

try this code

$.ajax({
    url: "./data.json", // or "/data.json",
    type: "GET",
    dataType: "json"
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.