2014-11-01 58 views
0

它非常简单的流星素描,我想在这里做一个远程API调用,以访问envato api流星js的http远程API访问:我在这里做错了什么?

但我所得到的是'未定义'。

我相信它很简单,但我只是无法弄清楚。任何帮助都是极好的。

下面是代码:

if (Meteor.isClient) { 
     // counter starts at 0 
     Session.setDefault("counter", 0); 

     Template.hello.helpers({ 
      counter: function() { 
       return Session.get("counter"); 
      } 
     }); 

     Template.hello.events({ 
      'click button': function() { 
       // increment the counter when button is clicked 

       Meteor.call("account", function(error, result) { 

        console.log(result); 

       }); 


       Session.set("counter", Session.get("counter") + 1); 
      } 
     }); 
    } 

    if (Meteor.isServer) { 
     Meteor.startup(function() { 
      // code to run on server at startup 
     }); 


     Meteor.methods({ 
     account: function() { 

      // Returns 'undfined' 
      //return HTTP.get("http://marketplace.envato.com/api/edge/USERNAME/APIKEY/account.json").data; 

      //works just fine. 
      return HTTP.get("http://marketplace.envato.com/api/edge/total-users.json").data; 

     } 
     }); 


    } 

有趣的是,它工作正常,当我使用它不需要USERNAME和APIKEY例如公共API集http://marketplace.envato.com/api/edge/total-users.json

我花了足够的时间和双重检查了一切。我在这里错过了什么?想法?

回答

0

所以最后我解决了它,因为它证明了;就像github .... envato api也需要用户代理标题;但是可以在没有用户代理标题的情况下访问envato的公共API集。

这里是代码:

Meteor.methods({ 
    account: function() { 

     // Returns 'undefined' until user agent header is present... as follows 

    result = Meteor.http.get('http://marketplace.envato.com/api/edge/USERNAME/APIKEY/account.json',{ 

    headers: {"User-Agent": "Meteor/1.0"} 

    }); 

    return result; 

     //following call works just fine without user agent header 
     //return HTTP.get("http://marketplace.envato.com/api/edge/total-users.json").data; 

    } 
    }); 
0

HTTP在服务器上已经是同步的。您可以使用此为您的方法,而不是:

Meteor.methods({ 
    account: function() { 
     return HTTP.get("http://marketplace.envato.com/api/edge/USERNAME/APIKEY/account.json").data; 
    } 
}); 

我不确定这是否会帮助您的问题,但它肯定会有所帮助。另外一定要添加httpmeteor add http

+0

由于Akshat ...它仍然是相同的应将来用于尝试帮助它“未定义”? – 2014-11-01 04:03:22

+0

@ user1348976我给你的代码对我来说完全和上面一样。如演示在这里:http://meteorpad.com/pad/7J2D6bAWA87QiMKbu。请同时检查您是否添加了http包 – Akshat 2014-11-01 06:27:41

+0

正如您在共享的Meteorpad上的API调用一样: return HTTP.get(“http://marketplace.envato.com/api/edge/total-users.json”) 。数据; 工作般的魅力,因为开始 但下面的API调用返回“未定义” 回报HTTP.get(“http://marketplace.envato.com/api/edge/RisingThemes/no1keq2xhphjw9ntgwdq0urav6fdeqm2/account.json”)。数据; 什么可能是错的?我检查了HTTP封装和凭据是正确的,点击curl http://marketplace.envato.com/api/edge/RisingThemes/no1keq2xhphjw9ntgwdq0urav6fdeqm2/account.json 正常工作。 – 2014-11-01 07:24:15

相关问题