我怎样才能加载本地的JSON文件? [英] How can I load a local json file?

查看:176
本文介绍了我怎样才能加载本地的JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来配置firefox使用about:config来允许本地文件访问(用于演示的目的)?



具体使用FF12 ,我需要能够有本地文件访问JSON数据。它在服务器上运行良好,但是我想让这个演示更加便携。

  $ .ajax({
url:../_assets/levelschema.json,
完成:function(data){
Levels = data.levels;
// ...
},
success:function(data){
// wont被称为b / c文件没有200 HTTP状态
},
async:false
});

我试过把它设置为不是async = false,但是我得到了否认错误。这是一个安全功能。我真的需要演示脱机工作,没有互联网接入,我宁愿使用它的人不必安装一个Web服务器。我还希望不必将所有数据嵌入到HTML标记中,因为这些数据可能会发生变化。

解决方案

使用AJAX的JSON请求工作得很好,只要您的数据文件是源于您的HTML文件的向下路径即可。所以我正在尝试升级到_assets /目录。解决方案是将_assets移动到HTML当前目录中进行访问。



在这种情况下,您可以使用带有async = false的AJAX。

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.

解决方案

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.

这篇关于我怎样才能加载本地的JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆