【问题标题】:How to handle response from HTTP Post如何处理来自 HTTP Post 的响应
【发布时间】:2017-02-25 06:20:04
【问题描述】:

我正在使用带有 Node.js 的 Angular 2,并且我正在尝试集成 Braintree,这并非易事。

我发现从事务返回结果对象而不重定向页面的唯一解决方案是让 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 方法的服务器返回,并使用初始数据创建一个对象。

另外,如果重要的话,我的服务器和前端位于不同的端口上。

【问题讨论】:

    标签: node.js http angular


    【解决方案1】:

    如果我正确理解您的问题,您需要为 .subscribe 方法提供一个回调函数,以便执行您想做的事情。回调应采用成功和错误函数,例如):

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

    【讨论】:

    • 看起来这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2011-10-14
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多