Meteor http.call在服务器上无法正常工作

时间:2013-07-09 14:25:24

标签: meteor

在Meteor中,我正在请求外部API(这个 - http://sciencesoft.at/latex/?lang=en)。

从客户端(控制台)执行Meteor.http.call时,它可以正常工作。当从服务器执行相同操作时,Meteor.http.call似乎发送一个空主体调用(忽略其选项参数)。

我使用的完整和具体的代码如下所示。在这里,我试着指出问题的原理:

在客户端(在控制台中),这很好用:

Meteor.http.call('PUT', 'http://sciencesoft.at/latex', {content: sentxml}, function (e,r) {console.log(r.content)}); //asynchronously

这里的sentxml包含xml请求的主体(在外部API的文档中指定)。

在服务器上,我有一个Meteor.method,其中包含:

return Meteor.http.call('PUT', 'http://sciencesoft.at/latex', {content: sentxml}); //synchronously

当我在控制台中这样做时:

Meteor.call('myMethod', sentxml, function (e,r) {          
  console.log(r.content);                              
});

我收到一个包含错误的xml响应(与我在方法中省略Meteor.http.call()的第三个参数时的错误相同)。


更具体地说,完整的代码如下。

当我在控制台中这样做时:

src= 'ABCD'; // I want to get png of this text.
latexsrc = '\\documentclass[12pt]{article}\\pagestyle{empty}\\begin{document}'+ src +'\\end{document}'; // minimalistic LaTeX source code
sentxml = '<?xml version="1.0" encoding="UTF-8"?><latex ochem="false"><dev dpi="120">png16m</dev><src><![CDATA['+latexsrc+']]></src><embeddedData>true</embeddedData></latex>'; // body of the xml request as described in the docs of http://sciencesoft.at/latex/?lang=en
Meteor.http.call('PUT','http://sciencesoft.at/latex', {content: sentxml}, function (e,r) {console.log(r.content)});

然后sentxml被正确发送到指定的url,我得到一个正确的xml响应,其中包含我请求的数据。

但是,我想从服务器进行http调用。我在Meteor方法中有一个几乎完全相同的代码:

if (Meteor.isServer) {
  Meteor.methods({
    getLatexImgData: function (src) {
      this.unblock();    
      var latexsrc = '\\documentclass[12pt]{article}\\pagestyle{empty}\\begin{document}'+ src +'\\end{document}';
      var sentxml = '<?xml version="1.0" encoding="UTF-8"?><latex ochem="false"><dev dpi="120">png16m</dev><src><![CDATA['+latexsrc+']]></src><embeddedData>true</embeddedData></latex>';
      var result = Meteor.http.call('PUT','http://sciencesoft.at/latex', {content: sentxml});      
      return result;    
    }
  });
}

我现在在控制台做的事情:

src = 'ABCD';
Meteor.call('getLatexImgData', src, function (e,r) {          
  console.log(r.content);                              
});

然后r.content包含一个xml响应,错误消息“Element'src'为空!没有可用的LaTeX源!” (与使用空体制作http.call的消息相同)。

1 个答案:

答案 0 :(得分:0)

我认为这是您的API的问题,而不是Meteor。

这个http调用似乎适用于我(服务器):

Meteor.http.call('PUT','http://sciencesoft.at/latex', {content: sentxml, params: {src: latexsrc}});
相关问题