1

I have a local json file in my web directory and want to access it. I currently use $.getJSON() from jquery and it works fine with all browsers but chrome.

How can I access this file via chrome?

Code:

<script>
        $(document).ready(function () {
            $.getJSON('JsonData.json', function (data) {
                //using data for stuff..
            });
        })

</script>

Edit: totaly forgot to paste the chrome console error here:

Access to XMLHttpRequest at 'file:///C:/Users/Sirzento/Desktop/website/JsonData.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.

sirzento
  • 537
  • 1
  • 5
  • 23

1 Answers1

4

You need to boot a webserver and serve content on a local port like 3000, and then access your site through localhost:3000. Chrome is refusing to allow you to make AJAX requests to file:// URLs, which is a security risk.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Does that mean if I want to access the site via internet that I must provide the port 3000 in the url? like 'DomainName:3000' ? – sirzento Jun 10 '19 at 15:02
  • No, when you publish the site to the Internet your Internet-facing webserver would be listening on port 80, and you would just use `http://DomainName`. – user229044 Jun 10 '19 at 15:03
  • But will this still work then if I access it via port 80 over the internet? – sirzento Jun 10 '19 at 15:09
  • I'm not really sure what you're asking here. Will *what* work, making XHR or fetch requests to port 80? Yes. – user229044 Jun 10 '19 at 15:13