【发布时间】:2014-06-21 03:24:52
【问题描述】:
我正在尝试从流星中的 vimeo API 返回一些 json 数据,并且在回调和服务器与客户端的东西方面遇到了一些问题,这是我的以下代码,但它不起作用,因为它可以看到变量中的 http 请求在客户端的服务器端,但如果我把它放在客户端我得到一个阻塞错误
if (Meteor.isServer) {
var auth_url='http://vimeo.com/api/v2/video/92861889.json';
}
//invoke the server method
if (Meteor.isClient) {
result = Meteor.http.call("GET", auth_url);
var issue = JSON.parse(result.content);
console.log(issue);
}
更新:当前错误
Uncaught SyntaxError: Unexpected token < test.js?6011fceba7e0032b9fbc7fe1cec33965967603ed:24
(anonymous function) test.js?6011fceba7e0032b9fbc7fe1cec33965967603ed:24
(anonymous function) httpcall_client.js:63
(anonymous function) underscore.js:750
xhr.onreadystatechange
新代码
好的,所以我重新输入了所有代码,据我所知,问题是复制和粘贴时出现的不可见字符。然而,我得到了一个跨源错误,所以我做了更多的研究,发现这是进行回调的正确方法。但是我收到一个错误“无法读取未定义的属性“内容””,我们将不胜感激一些更多的帮助。出于测试目的,我为 json 选择了不同的 url。
if (Meteor.isServer) {
Meteor.startup(function () {
Meteor.methods({
'remoteGet' : function(url, options){
return HTTP.get(url,options);
}
});
});
}
if (Meteor.isClient) {
Meteor.call('remoteGet', 'http://www.kimonolabs.com/api/ahd8tq6q?apikey=6c55fc2ded8114a1c08b8a914851de84',{
},function(error,response){
var issue = JSON.parse(response.content);
console.log(issue);
});
}
【问题讨论】:
-
你能多说一点关于你希望如何调用它吗?我不清楚您是希望服务器调用
HTTP.get还是客户端......还是客户端请求服务器进行调用然后返回解析结果? -
@davidweldod 老实说我不确定哪个是最好的,我的主要目标是解析为 json 数据并将其输出到模板中,我只是想先测试一下是否可以甚至可以在控制台中查看数据。
标签: javascript json rest meteor vimeo