I’m building a game using HTML5 Canvas and Javascript and I’m using JSON formatted tile maps for my levels. The tiles render correctly in FireFox, but when I use Chrome, the JSON fetching fails with a “Origin null is not allowed by Access-Control-Allow-Origin.” I was using jQuery’s $.ajax command and all my files are in one directory.
I would use this post’s solution, but I can’t use the web server solution.
Is there any other way to fetch JSON files to be parsed and read from? Something akin to loading an image just by giving the URL? Or is there some way to quickly convert my JSON files into globally available strings so I can parse it with JSON.parse()?
Why is the local web server not an option? Apache is free, can be installed on anything, and easy to use, IMO. Also, for Chrome specifically, look into
--allow-file-access-from-filesBut if nothing else works, maybe you could just add links to the files in
scripttags, and then appendvar SomeGlobalObject = ...to the top of each file. You might even be able to do this dynamically by using JS to append the script tag tohead. But in the end, instead of using AJAX, you can just doJSON.parse(SomeGlobalObject)In other words, load the files into the global namespace by adding script tags. Normally this would be considered bad practice, but used ONLY for testing, in the absence of any other options, it may work.