【发布时间】:2017-10-28 14:06:16
【问题描述】:
我有这个 Meteor 方法:
Meteor.methods({
'RESTcall':function () {
this.unblock();
return Meteor.http.call("GET", "http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/soluzioniViaggioNew/228/458/2017-05-31T00:00:00");}});
还有一个 React Component.jsx,在渲染函数中有一个按钮,它调用这样的函数:
search(){
Meteor.call("RESTcall", (error, response)=>{
console.log(response); //this works
this.setState({results: response}); //this throws an exception
}
});
}
问题是如何使用回调函数中的响应来呈现其内容。
提前致谢。
【问题讨论】:
-
在您的代码中
console.log(results)但在任何地方都没有定义results。console.log(response)的输出是什么?又抛出了什么样的异常? -
抱歉,console.log (response) 有效,它是 JSON 响应
-
你能提供例外吗?你的电话看起来不错。 This.setState 应该可以工作,因为您使用的是箭头函数。我猜问题出在 http 调用上,可能是 CORS 问题。
标签: javascript api reactjs meteor methods