【问题标题】:Send Client side variable to Server in Meteor将客户端变量发送到 Meteor 中的服务器
【发布时间】:2015-09-22 14:45:37
【问题描述】:

似乎有这样的东西,但在opposite。这是closest,但我没有存储任何东西。

我只是在我的另一个post/answer 中使用条纹来收费:

客户:

我正在使用 jQuery 从输入字段中获取值:

<input type="text" id="amount" name="amount"/>
var amount = $("#amount").val() *100; // the js client

服务器:

要从卡中扣款,您需要:

amount: 7000, // I want to remove the static value

我需要这样的东西:

amount: amount

我还在学习 js,我认为 jquery 会起作用,但我想不会。我试过将amount 传递给isServer 函数,但这不起作用。有其他选择吗?

我也尝试过使用var amount = document.getElementById('#amount');document 没有定义。

编辑:

if (Meteor.isServer) {
  Meteor.methods({
    'chargeCard': function(stripeToken, amount) {
      check(stripeToken, Object);
      var stripe = Meteor.npmRequire('stripe')('sk_test_rand');

      stripe.customers.create({
        email: 'a@aol.com'
      }).then(function() {
        return stripe.charges.create({
          amount: amount,
          currency: 'gbp',
          source: stripeToken.id
        });
      }).then(function(err, charge) {
        // New charge created on a new customer
        console.log(err, charge);
      }, function(err) {
        // Deal with an error
        console.log('Card not charge')
      });
    }
  });
}

【问题讨论】:

  • 有什么特别的错误吗?您能否在调用 isServer 函数时提供完整的 jquery 代码。如果您还可以提及服务器端代码,那就太好了。
  • 我已经编辑了帖子。 @MarkUretsky 我会读一读。谢谢。
  • @Sylar 你在客户端有流星呼叫吗? Meteor.call('chargeCard', token, amount, function(err, res) { }
  • @MarkUretsky 没有。但只是将数量添加到 cal()l 中,它就起作用了。您可以将其添加为答案并且不接受它。谢谢!!

标签: javascript jquery meteor


【解决方案1】:

要从客户端调用服务器上的流星方法,您需要使用meteor.call

Meteor.call('chargeCard', token, amount, function (err, res) {
  if (err) {
    console.log(err);
    return;
  }
  // success
  // if you return anything from the server you can find it with res
  console.log(res);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多