2

Possible Duplicate:
XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin
XMLHttpRequest cannot load file://… Origin null is not allowed by Access-Control-Allow-Origin

I am trying to open my JSON file such as:

jQuery.getJSON('../data/json/en/nodesData.json',
      function(data){
           jQuery.each(data, function(){
                  //do something...
           })
     })

this yields the following error in Chrome:

XMLHttpRequest cannot load file:///C:/URL/data/json/en/nodesData.json. Origin null is not allowed by Access-Control-Allow-Origin

what's problem? How can I retrieve my json?

Community
  • 1
  • 1
  • Is this script on a page on your local machine or on the web? – Rocket Hazmat Oct 30 '12 at 16:06
  • I am working on my local machine. my application is based on Jquery –  Oct 30 '12 at 16:14
  • 1
    Can you install a webserver on your local machine? That would make this work, and make your life easier. http://www.apachefriends.org/en/xampp.html – Rocket Hazmat Oct 30 '12 at 16:15
  • No I cant, it is not goal of project what I am developping –  Oct 30 '12 at 16:18
  • What *is* your goal? Because if it involves AJAX calls, you're gonna want a web server. Without it you're gonna have a hard time, like you are seeing now. – Rocket Hazmat Oct 30 '12 at 16:19
  • 1
    My goal is to import data from JSON file within utilization a variable. I founded a way by AJAX calls to make import, but there is probleme for Chrome –  Oct 30 '12 at 16:29
  • @RocketHazmat I want to underline that loading a local JSON file is a reasonable goal. I want to do this too because I want to create a locally working webapp, that preloads data from a JSON file. I now ran into the very same problem than the OP. – Marcel Feb 14 '13 at 19:39
  • @Marcel: That's what localStorage and/or IndexedDB is for. It'd be a **MAJOR** security hazard if a web browser could load arbitrary files from your hard drive without your knowledge. – Rocket Hazmat Feb 14 '13 at 19:41
  • @RocketHazmat not "arbitrary", but a local file in the same directory, loaded from an html file, I just double-clicked on. But, talking about localStorage and IndexedDB, is there any way to prepare these. BEFORE my html page gets double-clicked? – Marcel Feb 14 '13 at 19:48
  • 1
    @Marcel: You prepare them in your page, before your code. (Oh, and there's the File System API too) – Rocket Hazmat Feb 14 '13 at 19:52
  • @Marcel: For security purposes, browsers restrict `file:///` urls from doing things. – Rocket Hazmat Feb 14 '13 at 19:54

3 Answers3

4

You really should be running a local server like Apache or IIS to run HTML/JavaScript code so you do not run into these restrictions.

You can start the browser up with the flag --allow-file-access-from-files which removes the restriction.

epascarello
  • 185,306
  • 18
  • 175
  • 214
2

For security reasons, you cannot make AJAX calls to your local file system. Some browsers allow this, but others don't. Chrome has a flag that enables this, but it's off by default.

Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323
1

Take a look into Same Origin Policy.

You cannot load the file because technically it is located in another domain. You need to call it like localhost/<Path>/nodesData.json.

Christoph
  • 46,101
  • 18
  • 92
  • 121
  • how I can call via localhost? I have not server, my application is based on Jquery –  Oct 30 '12 at 16:12
  • 1
    Try [this answer](http://stackoverflow.com/questions/3102819/chrome-disable-same-origin-policy) to make it work in chrome... – Christoph Oct 30 '12 at 16:16
  • @Valeriane did you look into the link i provided in my last comment and tried to make it owrk? – Christoph Oct 31 '12 at 17:24