Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用node.js调用chargebee等API_Javascript_Node.js - Fatal编程技术网

Javascript 使用node.js调用chargebee等API

Javascript 使用node.js调用chargebee等API,javascript,node.js,Javascript,Node.js,在node.js上调用API时,我有点困惑。 我有一个运行node js的服务器,在那里我可以安装类似chargebee的框架 我创建了一个html页面,在其中进行订阅等。现在我想调用相应的chargebee函数进行订阅。 如果我尝试使用require('chargebee')加载chargebee,则会失败。我只能在服务器js中加载它 那么,我怎样才能使用chargebee的功能呢 我是否可以通过单击按钮从chargbee调用函数?我必须通过快递提供此功能吗 谈到node.js,我想我不理解客

在node.js上调用API时,我有点困惑。 我有一个运行node js的服务器,在那里我可以安装类似chargebee的框架

我创建了一个html页面,在其中进行订阅等。现在我想调用相应的chargebee函数进行订阅。 如果我尝试使用
require('chargebee')
加载chargebee,则会失败。我只能在服务器js中加载它

那么,我怎样才能使用chargebee的功能呢

我是否可以通过单击按钮从chargbee调用函数?我必须通过快递提供此功能吗

谈到node.js,我想我不理解客户端代码和服务器端代码之间的区别。
例如,如何通过单击html按钮来调用服务器端的功能

为了触发客户端的请求,您可以使用表单或AJAX。下面是一个使用express framework的示例,其中表单用于触发chargebee中的请求并创建订阅

客户端代码:

<html>
   <body>
      <form action="/subscribe" method="post">
            <label for="name">First Name:</label>
            <input type="text" id="name" name="customer[first_name]" placeholder="first name" />
            <br />
            <label for="name">Last Name:</label>
            <input type="text" id="name" name="customer[last_name]" placeholder="last name" />
            <br />
            <label for="email">Email:</label>
            <input type="email" id="email" name="customer[email]" placeholder="Enter your email address" />
            <br />
            <input type="submit" value="Create Profile" />
      </form>
   </body>
</html>

名字:

姓氏:
电邮:
节点服务器代码:

var express = require('express');
var chargebee = require("chargebee");
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

chargebee.configure({site : "<<site_name>>",
  api_key : "<<api_key>>"

app.get('/', function(req, res){
  res.sendFile(__dirname + '/form.html');
});

app.post('/subscribe', function(req, res){
      var params = req.body;// getting form params as JSON
      params['plan_id']='enterprise'; // plan id that is present in your Chargebee site
      chargebee.subscription.create(params).request(function(error,result){
        if(error){
          //handle error
          console.log(error);
        }else{
          console.log(result);
          var subscription = result.subscription;
          res.writeHead(200, {
             'content-type': 'text/plain'
          });
          res.write('Successfully created subscription\n\n' + 'id :: '+ subscription.id);
          res.end();
       }
     });
});

app.listen(3000);
console.log("server listening on 3000");
var express=require('express');
var chargebee=要求(“chargebee”);
var bodyParser=require('body-parser');
var-app=express();
app.use(bodyParser.json());//支持JSON编码的实体
使用(bodyParser.urlencoded({//)来支持URL编码的实体
扩展:正确
}));
chargebee.configure({site:“”,
api_键:“
app.get('/',函数(req,res){
res.sendFile(uu dirname+'/form.html');
});
app.post('/subscribe',函数(req,res){
var params=req.body;//将表单参数获取为JSON
params['plan_id']='enterprise';//存在于Chargebee站点中的计划id
chargebee.subscription.create(params).request(函数(错误,结果){
如果(错误){
//处理错误
console.log(错误);
}否则{
控制台日志(结果);
var subscription=result.subscription;
文书标题(200{
“内容类型”:“文本/普通”
});
res.write('已成功创建订阅\n\n'+'id::'+subscription.id);
res.end();
}
});
});
app.listen(3000);
log(“服务器监听3000”);

为了触发客户端的请求,您可以使用表单或AJAX。下面是一个express framework的示例,其中表单用于触发请求并在chargebee中创建订阅

客户端代码:

<html>
   <body>
      <form action="/subscribe" method="post">
            <label for="name">First Name:</label>
            <input type="text" id="name" name="customer[first_name]" placeholder="first name" />
            <br />
            <label for="name">Last Name:</label>
            <input type="text" id="name" name="customer[last_name]" placeholder="last name" />
            <br />
            <label for="email">Email:</label>
            <input type="email" id="email" name="customer[email]" placeholder="Enter your email address" />
            <br />
            <input type="submit" value="Create Profile" />
      </form>
   </body>
</html>

名字:

姓氏:
电邮:
节点服务器代码:

var express = require('express');
var chargebee = require("chargebee");
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));

chargebee.configure({site : "<<site_name>>",
  api_key : "<<api_key>>"

app.get('/', function(req, res){
  res.sendFile(__dirname + '/form.html');
});

app.post('/subscribe', function(req, res){
      var params = req.body;// getting form params as JSON
      params['plan_id']='enterprise'; // plan id that is present in your Chargebee site
      chargebee.subscription.create(params).request(function(error,result){
        if(error){
          //handle error
          console.log(error);
        }else{
          console.log(result);
          var subscription = result.subscription;
          res.writeHead(200, {
             'content-type': 'text/plain'
          });
          res.write('Successfully created subscription\n\n' + 'id :: '+ subscription.id);
          res.end();
       }
     });
});

app.listen(3000);
console.log("server listening on 3000");
var express=require('express');
var chargebee=要求(“chargebee”);
var bodyParser=require('body-parser');
var-app=express();
app.use(bodyParser.json());//支持json编码的实体
使用(bodyParser.urlencoded({//)来支持URL编码的实体
扩展:正确
}));
chargebee.configure({site:“”,
api_键:“
app.get('/',函数(req,res){
res.sendFile(uu dirname+'/form.html');
});
app.post('/subscribe',函数(req,res){
var params=req.body;//将表单参数获取为JSON
params['plan_id']='enterprise';//存在于Chargebee站点中的计划id
chargebee.subscription.create(params).request(函数(错误,结果){
如果(错误){
//处理错误
console.log(错误);
}否则{
控制台日志(结果);
var subscription=result.subscription;
文书标题(200{
“内容类型”:“文本/普通”
});
res.write('已成功创建订阅\n\n'+'id::'+subscription.id);
res.end();
}
});
});
app.listen(3000);
log(“服务器监听3000”);

使用chargebee v3是可能的。希望这能解决您的问题


chargebee示例

使用chargebee v3是可能的。希望这能解决您的问题


chargebee示例

对于客户端和服务器端js之间的差异,曾经运行@ur browser的是客户端,您可以通过查看源代码/查看浏览器代码工具找到。曾经通过server.js/what so运行@command line code的是服务器端代码。对于客户端和服务器端js之间的差异,曾经运行@ur browser的是客户端Chu可以通过查看源代码/检查浏览器代码的工具来查找。通过server.js/what so.运行@command line代码的是什么?服务器端代码..Thx是解决方案。那么没有办法直接访问chargbee函数吗?我的意思是,可以将require作为外部js加载到库中并调用函数吗?我不确定questions。您想直接从客户端脚本访问函数吗?您的答案非常好。我只需要了解是否可能。为什么chargebee函数不能在用户单击时直接调用。无法直接从客户端(单击时)调用函数.Chargebee节点库是一个api包装器,用于使用基本身份验证从服务器调用api,不能在客户端使用。直接在客户端工作的Javascript库与node js不同。您可以参考此Thx了解解决方案。因此,是否无法直接访问chargbee函数?我的意思是,是否可以使用需要作为外部js并调用函数?我不确定这些问题。你想直接从客户端脚本访问函数吗?你的答案非常好。我只需要了解是否可能。为什么chargebee函数