How to get JSON locally without AJAX

I create a game using HTML5 Canvas and Javascript, and I use JSON formatted tile maps for my levels. Tiles display correctly in FireFox, but when I use Chrome, JSON fetching is not performed using "Original Zero" Access-Control-Allow-Origin is not allowed. "I used the jQuery $ .ajax command, and all my files are in the same directory.

I would use this post-solution, but I cannot use the web server solution.

Is there any other way to get JSON files for analysis and reading? Something like uploading an image just by providing a url? Or is there a way to quickly convert my JSON files to globally accessible strings, so I can parse it with JSON.parse ()?

+4
source share
2 answers

Why is a local web server not an option? Apache is free, can be installed on anything and is easy to use, IMO. Also, for Chrome, look specifically at --allow-file-access-from-files

But if nothing works, maybe you can just add file links in script tags, and then add var SomeGlobalObject = ... to the top of each file. You can even do this dynamically using JS to add a script tag to head . But in the end, instead of using AJAX, you can just do JSON.parse(SomeGlobalObject)

In other words, upload the files to the global namespace by adding script tags. This is usually considered bad practice, but is used ONLY for testing, in the absence of any other options it can work.

+1
source

One option that might work for you in Chrome is to invoke the browser using the command line --allow-file-access-from-files . This question solves the problem: Google Chrome - file access disabled due to files for Chrome Beta 8

Another possibility is to get JSON data as a script by setting the global variable to JSON

+1
source

Source: https://habr.com/ru/post/1441055/


All Articles