javascript - 如何使用 Meteor.http.call

标签 javascript meteor

我正在尝试使用 Meteor.http.call 发送短信。我遇到两个错误:

First error:When page loaded,"WebSocket connection to 'ws://localhost:3000/sockjs/632/i0uapg48/websocket' failed: WebSocket is closed before the connection is established."

Second error:when I click ebultenkydet,"Uncaught TypeError: Cannot read property 'call' of undefined"

Template.footerLayout.events({
'click #ebultenkaydet': function(e, template) {
var auth_url="http://api.sorentesms.com/index.php"
var result = Meteor.http.call("POST", auth_url, {
data: {
              'apiNo':'1',
              'user':'test',
              'pass':'test123',

              'message':'hi',
              'number':'+905075587***',
              'from':'test',

           },
           headers: {
                 "content-type":"application/json",
                 "Accept":"application/json"
           },
         })
         }
 });

你能帮我解决一下吗? 谢谢大家

最佳答案

您正在客户端 block 内发送 http 请求,并且 Meteor.http 仅在服务器端可用。您必须将此 block 放入 Meteor.isServer block 中。

不要忘记meteor添加http才能使用代码:

让我重写你的代码:

if (Meteor.isServer) {
  Meteor.methods({
    authCall: function () {
      this.unblock(); // Make sure server doesn't get block from this call
      var auth_url="http://api.sorentesms.com/index.php";
      return Meteor.http.call("POST", auth_url, {
        data: {
          'apiNo':'1',
          'user':'test',
          'pass':'test123',
          'message':'hi',
          'number':'+905075587***',
          'from':'test',
        },
        headers: {
          "content-type":"application/json",
          "Accept":"application/json"
        },
      })
    }
  });
}


Template.footerLayout.events({
'click #ebultenkaydet': function(e, template) {
    Meteor.call("authCall", function(error, results) {
        console.log(results); //results.data should be a JSON object
    });
});

关于javascript - 如何使用 Meteor.http.call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401952/

相关文章:

javascript - 调用 Meteor.call() 并在 `before: insert` 钩子(Hook)内等待

javascript - ReactJS 与传单 : tiles not correctly displayed until page refresh

javascript - 如何将javascript变量分配给jsp或jSTL

javascript - 如果没有 "link_to"类助手,我如何调用 ajax?

node.js - meteor 观察阵列服务器端

javascript - 上传到 meteor 中的 Amazon s3 未定义属性

javascript - 在 asp.net 中单击按钮时页面刷新

.net - Visual Studio 2008 和覆盖现有文件扩展名的语言服务

javascript - 在对象函数中定义 getter/setter

javascript - 如何在嵌套字段中保存Mongo文档自己的_id?