2017-06-06 248 views
1

我有一個離子應用程序,我試圖發送我的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!') 
}) 

回答

1

我認爲你需要映射日訂閱請求

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'; 
0

我將在stripe.charges.create回調試試這個,看看會發生什麼:

}, function(err, charge) { 
    if (err) throw err; 

    console.log('Charge ID:', charge.id); 

    res.send('Hello World!'); 
}); 
+0

錯誤消息來了從'cart.ts',它不會發出請求...它沒有到達節點服務器。節點服務器本身功能正確。 – maudulus

+0

什麼是錯誤信息?您的帖子沒有包含錯誤消息。 – floatingLomas