如何处理来自HTTP Post的响应

我正在使用Angular 2与Node.js,我正在尝试整合braintree,这并非简单的任务。

我发现从事务中返回结果对象而不redirect页面的唯一解决scheme是让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()? 我需要添加到服务器function?

总之,我需要使用HTTP Post方法来发布数据,并等待来自接受HTTP Post方法的服务器的对象,并使用初始数据创build一个对象。

另外,万一它很重要,我的服务器和前端在不同的端口。

如果我正确理解你的问题,你需要给.subscribe方法一个callback函数,以便做你想做的事情。 例如callback应该是成功和错误的函数):

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