【问题标题】:Extract NodeJS Response into HTML将 NodeJS 响应提取到 HTML
【发布时间】:2014-08-14 05:58:41
【问题描述】:

我想在 NodeJS 上接受这个请求

Stripe.apiKey = "sk_test_AJKebDsM6QFiK8VpJ06Vq1M1";

stripe.balance.retrieve(function(err, balance) {
// asynchronously called
});

示例响应是什么

{
  "pending": [
{
  "amount": 0,
  "currency": "usd"
}
 ],
 "available": [
{
  "amount": 0,
  "currency": "usd"
}
],
 "livemode": false,
"object": "balance"
}

并提取余额信息以将其放入 HTML 标签中。

我该怎么做呢?我可以将 response 传递给 Angular,然后将其链接到 HTML 吗?

【问题讨论】:

    标签: javascript html node.js angularjs stripe-payments


    【解决方案1】:

    您必须在路线中提供服务(例如/balance),然后让您的 Angular 应用程序使用它:

    如果您使用 express 作为您的网络框架,这里有一个示例(来自我的脑海 - 未经测试):

    app.get('/balance', function (request, response) {
      stripe.balance.retrieve(function(err, balance) {
        response.send(balance);
      });
    });
    

    这里是你的 Angular 应用程序:

    angular.module('myApp', [])
    .controller('BalanceController', function ($scope, $http) {
      $http.get('/balance').then(function (response) {
        $scope.balance = response.data;
      });
    });
    

    【讨论】:

    • 太棒了!谢谢,时间允许时会检查的。
    • 试过了,但它在节点文件中返回了 app.get 的错误。这是错误:Uncaught ReferenceError: app is not defined 为什么会这样?
    • 是的,我要设置var app = express();吗?
    • 嗯,是的...你是如何运行你的服务器的?我假设你有一个入口点(index.jsserver.js...)并像 node index.jsnode server.js 一样运行它。它的内容至少应该是var express = require('express'); var app = express(); app.listen(3000 /*or some other port*/); 然后,在收听之前,您将添加我的答案中的代码。
    • 我正在这样做,但现在它说 required 未定义。有什么简单的解决方案吗?
    猜你喜欢
    • 2019-06-08
    • 2022-01-21
    • 2017-08-30
    • 2020-07-14
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2012-11-07
    相关资源
    最近更新 更多