gpt4 book ai didi

javascript - 如何将 json 数据传递给 meteor 模板

转载 作者:行者123 更新时间:2023-11-30 10:14:40 25 4
gpt4 key购买 nike

我正在尝试将数据从 json rest api 传递到 meteor 模板

我像这样从 HTTP GET 获取 JSON:

if (Meteor.is_client) {

Meteor.http.call("GET", "https://public-api.wordpress.com/rest/v1/freshly-pressed", function (err, result){
console.log(result.content);
})
}

if (Meteor.is_server) {

}

我可以在浏览器控制台中看到 JSON 数据

如何将数据传递给模板?

最佳答案

有几种方法,具体取决于您调用电话的位置和您使用的包。最简单的一种是使用 session 和一个助手:

HTTP.get(..., function(err, result) {
Session.set('httpResult', result);
});

Template.myTemplate.json = function() {
return Session.get('httpResult');
};


<template name="myTemplate">
{{json.property}}
{{#with json}}
{{property}}
{{otherProperty}}
{{lotsOfProperties}}
{{/with}}
</template>

关于javascript - 如何将 json 数据传递给 meteor 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com