0

I've been trying to figure this thing out for a while now. The users.json file I'm using is located in "JSON" folder right next to the rest of the files. alert("Before") is working but I can't get the alert("After") to work :/

If anyone could help me I'd be grateful for life.

main.js

$(document).ready(readJSONFile);

function readJSONFile() {
    alert("Before");
    $.getJSON("JSON/users.json", function(data){
        alert("After");
        let options ='';
        for(let i= 0; i<data.length;i++){
            options += `<option value="${data[i].firstName}">${data[i].firstName}</option>`;
        }
        $('#users').html(options);
    });
}

users.json

[
    {
        "firstName": "John",
        "lastName": "Doe",
        "address": "Del Mar"
    },
    {
        "firstName": "Jonah",
        "lastName": "Hill",
        "address": "Jerusalem"
    },
    {
        "firstName": "James",
        "lastName": "Bond",
        "address": "San Jose"
    }
]
Mitch Tal
  • 41
  • 7
  • 2
    Are you getting errors in your console? (F12 in your browser) – Andy Aug 11 '19 at 17:51
  • 5
    And while you are there look in network tab and inspect the actual request – charlietfl Aug 11 '19 at 17:52
  • jquery.min.js:2 Access to XMLHttpRequest at 'file:///C:/Users/.../JSON/users.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. – Mitch Tal Aug 11 '19 at 18:22
  • Well then @MitchTal there is your answer. You need to just solve the cors issue. I would say to just try a local path and see what happens. – Fallenreaper Aug 11 '19 at 21:26

1 Answers1

0

I think you just left off a ./ to indicate that the path is relative to the current path.

$.getJSON("./JSON/users.json", function(data){
Bill
  • 251
  • 1
  • 4