FlavorScape
FlavorScape

Reputation: 14309

How can I load a local json file?

Is there a way to configure firefox using about:config to allow local file access (for demo purposes)?

Specifically using FF12, I need to be able to have local file access to json data. It runs fine on a server, but I'd like to make this demo more portable.

        $.ajax({
            url: "../_assets/levelschema.json",
            complete: function (data) {
                Levels = data.levels;
                //...
            },
            success: function (data) {                    
                // wont get called b/c files don't have 200 HTTP status
            },
            async: false
        });

I've tried setting it to not async=false, but I get a "Access to restricted URI denied" error. This is a security feature. I really need the demo to work offline, without internet access, and I'd prefer the people using it not have to install a web server. I'd also prefer not having to embed all my data in the HTML tags since that data subject to change.

Upvotes: 2

Views: 7623

Answers (3)

Boris Zbarsky
Boris Zbarsky

Reputation: 35074

You can flip the security.fileuri.strict_origin_policy preference in about:config. But that opens up some security holes; moving your JSON to be in a child directory of the directory the HTML is in is a much better idea.

Upvotes: 5

FlavorScape
FlavorScape

Reputation: 14309

Ok, JSON requests using AJAX work just fine as long as your data files are down path from your originating HTML file. So I was trying to go up a level to _assets/ directory. The solution is to move _assets into the HTML current directory for access.

You can use AJAX with async=false in this case.

Upvotes: 1

woz
woz

Reputation: 11004

If the JSON file is on your local computer, access it like this:

<script type="text/javascript" src="myFile.json"></script>

You'll have to add that line to your offline project and remove it when it's live, but it's the best way to do it without any server-side assistance. You'll also have to remove your AJAX code for your demo, because it's not going to work on your local computer.

Upvotes: 1

Related Questions