0

I am trying to load data from JSON file in the same folder, here is my javascript code:

$.getJSON('JsonFile.json', function(data){
alert("here");
var a = data[0];
});

but when I open this html in chrome, it throws error :

XMLHttpRequest cannot load file:///C:/UserskkDesktopchartsJsonFile.json. Cross origin requests are only supported for HTTP. jquery.min.js:4
o.ajaxTransport.l.cors.a.crossDomain.send jquery.min.js:4
o.extend.ajax jquery.min.js:4
o.each.o.(anonymous function) jquery.min.js:4
o.extend.getJSON jquery.min.js:4
(anonymous function) chart.html:28

Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/UserskkDesktopchartsJsonFile.json'. jquery.min.js:4
o.extend.ajax jquery.min.js:4
o.each.o.(anonymous function) jquery.min.js:4
o.extend.getJSON jquery.min.js:4
(anonymous function)

Which shows I fail to load this JSON file, anyone knows the reason, thanks a lot

user3527917
  • 327
  • 2
  • 10
  • 22
  • 2
    You cannot use AJAX if you are not running your page on a webserver. – gen_Eric Jun 24 '14 at 20:36
  • possible duplicate of [Cross-site AJAX requests](http://stackoverflow.com/questions/333532/cross-site-ajax-requests) – edi9999 Jun 24 '14 at 20:38
  • 1
    You need to pass the `--allow-file-access-from-files` parameter to your Chrome executable when you run it. – Bucket Jun 24 '14 at 20:39
  • 2
    Ajax and the [SOP](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) don't typically support `file://`. You'll want to setup an `http://` server to host your files ("*Cross origin requests are only supported for HTTP.*"). [There are numerous options for lightweight, static file servers](http://stackoverflow.com/questions/5050851/best-lightweight-web-server-only-static-content-for-windows). – Jonathan Lonowski Jun 24 '14 at 20:40

0 Answers0