Javascript 如何使用Meteor和React进行API调用

Javascript 如何使用Meteor和React进行API调用,javascript,api,reactjs,meteor,methods,Javascript,Api,Reactjs,Meteor,Methods,我有一个流星法: 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 Com

我有一个流星法:

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,在render函数中有一个按钮,调用如下函数:

   search(){
      Meteor.call("RESTcall", (error, response)=>{
                 console.log(response); //this works
                 this.setState({results: response}); //this throws an exception
            }
      });
}
问题是如何使用回调函数的响应来呈现其内容


提前谢谢。

您可以做一个小改动,如下所示。我还没有测试过它,但我认为它应该可以工作

 search(){
      var that = this; /*Since the setState method in this is accessible here*/
      Meteor.call("RESTcall", (error, response)=>{
                 console.log(response); 
                 that.setState({results: response});
            }
      });

在您的代码中,您可以
console.log(results)
但在任何地方都没有定义
结果。控制台日志(响应)
的输出是什么?抛出了什么样的异常?抱歉,是的,console.log(response)可以工作,它是JSON响应。您能提供异常吗?你的电话听起来不错。此.setState应该可以工作,因为您使用的是箭头函数。我猜问题是来自http调用,可能是CORS问题。他使用的是arrow函数,所以这应该是react类的绑定,this.setState应该可以工作。