如何使用角度捕获数据并发送到服务器 API
How to capture data and send to server API using angular
我是第一次使用 Angular,在将简单数据发送回服务器时遇到了问题。
我有一个在 API: http://localhost:8080/takepayment
上运行的服务器,它接受POST
请求并期望tokenId
.
我的 JS 看起来像这样,并且成功从 Stripe 取回了一个令牌。我只想将此令牌作为POST
请求发送到上述 API URL。
如何在 Angular 中执行此操作?
'use strict';
angular.module('angularDjangoRegistrationAuthApp')
.controller('TakeMoneyCtrl', function ($scope, djangoAuth, Validate) {
$scope.complete = false;
djangoAuth.profile().then(function(data){
$scope.model = data;
});
$scope.handleStripe = function(status, response){
if(response.error) {
// there was an error. Fix it.
alert("error")
} else {
// got stripe token, now charge it or smt
token = response.id
alert("success: " +token);
}
}
});
我想仅在成功从 Stripe 取回令牌时才将令牌发送到服务器。所以我在那里放了一个alert("success");
,但即使是那个警报也没有显示在页面上。alert("error");
也没有显示。但是,在chrome的网络选项卡上,我可以看到token
实际上是从条纹返回的。
问题
如何将从 Stripe 获取的令牌作为POST
请求发送到我的 API 调用http://localhost:8080/takepayment
/
您需要
使用 Angular $http
服务(此处为文档)。注入它,然后在控制器中使用它。下面是一个简单的示例,它使用 Angular $http
服务并执行POST
。
angular.module('angularDjangoRegistrationAuthApp')
.controller('TakeMoneyCtrl', ['$http', function ($scope, $http, djangoAuth, Validate) {
$scope.complete = false;
djangoAuth.profile().then(function(data){
$scope.model = data;
});
$scope.handleStripe = function(status, response){
if(response.error) {
// there was an error. Fix it.
alert("error")
} else {
// got stripe token, put it in an object then use it in $http.post
var dataModel = {
token: response.id
};
//You can do your $http post here OR preferably in Seperate Service.
$http.post('/someUrl', JSON.stringify(dataModel)).
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
alert("success: " +token);
}
}
}]);
以上是一种方法。但最好将$http
调用移动到单独的 Angular 服务中,并可能使用 JS Promise 来等待响应或超时发生。下面是一个服务和使用 Promise 进行 HTTP 发布的示例。
创建一个新的 Angular 服务来处理$http
var app = angular.module('angularDjangoRegistrationAuthApp', []);
app.service('CommService', ['$http', function ($http) {
this.processLoginToken = function(token) {
var model = {
token: token
};
var promise = $http.post('/api/tokenproc', JSON.stringify(model))
.then(function(response) {
return response.data;
}, function(error) {
console.log("Communication error, server did not reply.");
});
return promise;
};
现在,在您的服务之外,您将像这样使用它。
var token = "02394924924929424";
CommService.processLoginToken(token)
.then(function (response) {
//Process your response.data here
}, function () {
//Server didn't respond.
});
相关文章:
- 使用JSP从服务器检索和显示图像
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 客户端服务器REST API captcha实现
- 正在将base64 jpeg从input-type=file上传到服务器
- Webpack开发服务器和React服务器端渲染
- 提示使用服务器端事件处理程序激活JavaScript
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何使用Socket.io将命令从客户端发送到服务器
- jQuery blueimp文件上传:将N-1个文件上传到IE中的服务器
- 使用jasmine模拟对服务器的调用
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 如何使用Javascript将空数组发送到PHP服务器
- 如何在HTML中显示服务器对象变量
- 如何配置分析以将数据发送到我自己的服务器
- 我无法使用angularJs($http)访问服务器
- node.js http服务器并发问题
- Grunt:并发:服务器原因“;警告:任务;咖啡:服务器”;找不到&”;
- AJAX服务器端处理可以并发运行