问题描述
我正在运行Meteor 0.7.2,并希望在客户端加载JSON
文件.我在应用程序根目录的/private
文件夹中放置了一个JSON
文件,并使用以下代码将其正确地加载到服务器端:
I am running Meteor 0.7.2 and want to load a JSON
file in the client side. I have placed a JSON
file in /private
folder in the app root directory and it is loaded in server side without errors using following code:
var jsonData = JSON.parse(Assets.getText("myjson.json"));
但是我也想在客户端做同样的事情.在客户端加载应用程序后,我想在客户端加载JSON
文件.或者,如果无法加载JSoN
,那么我想在服务器端更新/分配Meteor.settings.pulbic
中的值,并且该更新需要客户端使用. (我阅读了Meteor文档,并参考了其他一些资源,但找不到解决方法.)
But I want to do the same thing in client side too. When the app is loaded in client side, I want to load a JSON
file at client side. Or if that JSoN
loading is not possible then I want to update/assign value in Meteor.settings.pulbic
in the server side and that update needs to be available to clients. (I read the Meteor documentation and refer several other resources, but couldn't figure out a way to do that.)
有人对以下事情有想法吗?
Can anybody have an idea on following things?
- 要在客户端放置要加载的文件的位置(类似于服务器的
/private
文件夹) - 应该使用哪种方法读取该文件(如果有
JSON
可以存储的地方) - 或者有一种方法可以在服务器端
Meteor.settings.public
加载后可供clents更新
- Where to place a file to be loaded in client side (same like
/private
folder for server) - What method should used to read that file (if there is a place for a
JSON
to store) - Or is there a way to update
Meteor.settings.public
from server side when it is loaded and available to clents
推荐答案
如果要使文件在客户端上可访问,请将其放在/public
中.
If you want to make files accessible on the client, put it in /public
.
例如,如果您创建文件/public/foo.json
,则可以在//server:port/foo.json
上访问该文件.您可以使用任何标准的HTTP和AJAX方法(包括Meteor的内置 HTTP.get
)加载该文件.
For example, if you create a file /public/foo.json
, it will be accessible at //server:port/foo.json
. You can load this file using any standard HTTP and AJAX methods, including Meteor's built-in HTTP.get
.
这篇关于如何在Meteor客户端中加载JSON文件或如何在服务器端更改Meteor.settings以供客户端使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!