2016-10-16 9 views
1

Node.jsでAngular 2を使用していますが、単純な作業ではないbraintreeを統合しようとしています。HTTP Postからの応答を処理する方法

ページをリダイレクトせずにトランザクションから結果オブジェクトを返す唯一の解決策は、HTTP Post要求でオブジェクトを返送することです。これは可能ですか?

onClick(){ 
var headers = new Headers(); 
headers.append('Content-Type', 'application/json'); 

var data = JSON.stringify({ 
    "nonce": localStorage.getItem('nonce'), 
    "amount": "5.88", 
    "firstName": this.model.firstName, 
    "lastName": this.model.lastName, 
    "customerId": this.model.userID, 
    "phoneNumber": this.model.phoneNumber, 
    "shippingStreet": this.model.shippingStreet, 
    "shippingCity": this.model.shippingCity, 
    "shippingZip": this.model.shippingZip, 
    "shippingState": this.model.shippingState, 
    "billingStreet": this.model.billingStreet, 
    "billingZip": this.model.billingZip, 
    "billingState": this.model.billingState, 
    "billingCity": this.model.billingCity, 
    "email": this.userEmail, 
    "date": this.date 
}); 

this.http.post('http://MyUrl.com:3300/checkouts', data, {headers: headers}) 
.subscribe(); 
console.log(data); 
console.log("Successfully submitted data to API server...");} 

.subscribe()に何かを追加する必要がありますか?サーバー機能に何を追加する必要がありますか?

要約すると、データをポストし、HTTP Postメソッドを受け入れたサーバーからオブジェクトが戻ってくるのを待って、初期データを持つオブジェクトを作成するには、HTTP Postメソッドが必要です。

また、重要な場合は、サーバーとフロントエンドが別のポートにあります。

答えて

1

私があなたの質問を正しく理解していれば、あなたがしたいことをするために.subscribeメソッドにコールバック関数を与える必要があります。コールバックには成功とエラー機能が必要です)。

this.http.post('http://MyUrl.com:3300/checkouts', data, {headers: headers}) 
    .subscribe(
     (response) => {console.log(response)}, 
     (error) => {console.log(error)} 
    ); 
+0

これはまさに私が探していたものでした。 –

関連する問題