发送http请求in Ionic with Angular http
问题描述:
我有一个离子应用程序,我试图发送我的Stripe
令牌,以进行付款处理。当我通过curl
将请求发送到node
服务器时,它正在接收请求。但是,当我尝试通过Angular的http模块发送请求时,它根本没有注册。所有这些目前都在本地进行测试,所以这可能是问题的一部分?发送http请求in Ionic with Angular http
HTML
<button (click)="testPost()" ion-button full>TEST POST</button>
cart.ts
...
import {Http, Headers, RequestOptions} from '@angular/http';
import { Stripe } from '@ionic-native/stripe';
@Component({
selector: 'page-cart',
templateUrl: 'cart.html',
})
export class Cart {
constructor(
...
private http: Http,
private stripe: Stripe
) {
//
});
}
testPost() {
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers });
let postParams = {
body: {
token: 'axqrexample123tokenbasdflkjo3',
amount: 225
}
}
this.http.post("http://11.1.1.7:3100/charge", postParams, options)
.subscribe(data => {
console.log(data['_body']);
}, error => {
console.log(error);// Error getting the data
});
}
}
节点服务器
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var stripe = require("stripe")("sk_test_abcTAdefgn1oDexamplex");
app.use(bodyParser.json());
app.post('/charge', function (req, res) {
var token = req.body.token;
var amount = req.body.amount;
stripe.charges.create({
amount: amount,
currency: "usd",
source: token, // obtained with Stripe.js
description: "Charge for [email protected]"
}, function(err, charge) {
// asynchronously called
});
res.send('Hello World!')
});
app.listen(3100, function() {
console.log('Example app listening on port 3100!')
})
答
我认为你需要映射日订阅请求
this.http.post("http://11.1.1.7:3100/charge", postParams, options)
.map(res => res.json())
.subscribe(data => {
console.log(data['_body']);
}, error => {
console.log(error);// Error getting the data
});
之前也E分别请求,导入rxjs
import 'rxjs/Rx';
答
我将在stripe.charges.create
回调试试这个,看看会发生什么:
}, function(err, charge) {
if (err) throw err;
console.log('Charge ID:', charge.id);
res.send('Hello World!');
});
错误消息来了从'cart.ts',它不会发出请求...它没有到达节点服务器。节点服务器本身功能正确。 – maudulus
什么是错误信息?您的帖子没有包含错误消息。 – floatingLomas