将本地json文件添加到rails app并使用$ .getJSON在本地获取它 [英] Add local json file to rails app and use $.getJSON to get it locally

查看:81
本文介绍了将本地json文件添加到rails app并使用$ .getJSON在本地获取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个本地静态JSON文件添加到我的rails应用程序,然后使用 $。getJSON 在同一个rails app中的另一个javascript文件中检索JSON数据。

I am trying to add a local, static JSON file to my rails app and then retrieve the JSON data using $.getJSON within another javascript file in the same rails app.

到目前为止,我尝试将其添加到 assets / javascripts / public / 然后运行 bundle exec rake assets:precompile ,但是我仍然没有成功将JSON文件加载到我的其他javascript文件中。

So far I tried adding it to assets/javascripts/ and public/ and then running bundle exec rake assets:precompile, however I still haven't successfully loaded the JSON file into my other javascript file.

推荐答案

您应该能够将静态JSON文件放在公共目录中并直接加载它。假设在 public 目录中有一个名为 static.json 的文件,您只需加载它:

You should be able to place a static JSON file in the public directory and load it directly. Assuming a file called static.json in the public directory, all you need to load it is:

$.getJSON('/static.json', function(data) {
  console.debug(data);
});

这篇关于将本地json文件添加到rails app并使用$ .getJSON在本地获取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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