【问题标题】:HTTP POST in Angular 2Angular 2 中的 HTTP POST
【发布时间】:2017-09-10 06:19:17
【问题描述】:

我在 Angular 2 中发布数据时遇到问题。

postOrder() {

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

    let data=JSON.stringify({
                          customer_id: this.cusid,
                          mode_code: this.data2code,
                          order_totalpayment: this.dataprice,
                          order_status: this.status,
                          order_remarks: this.remarks,
                          order_type: this.ordertype
                        });


  this.http.post('http://JSON-API',data,headers)
  .map(res => res.json())
  .subscribe(res => {
  console.log("Success Order No.: "+res);
  this.ordernum = res;
  });

        let data2=JSON.stringify({
                        order_no: this.ordernum,
                        prod_id: this.dataid,
                            order_product_quantity: this.quantity,
                            order_subtotal: this.dataprice
                            });

  this.http.post('http://JSON-API',data2,headers)
  .map(response => response.json())
  .subscribe(response => {
  console.log("Order List No.: "+response);
  console.log(this.ordernum);
  });

}

我的问题是我无法发布 res 数据,但是当我使用控制台日志时,它会显示正确的数据。我所做的是将res 数据传递给ordernum 变量。

this.http.post('http://JSON-API',data,headers)
      .map(res => res.json())
      .subscribe(res => {
      console.log("Success Order No.: "+res);
      this.ordernum = res;
      });

然后我尝试将 POST 发送到 order_no 到我的 JSON API。

let data2=JSON.stringify({
                        order_no: this.ordernum,
                        prod_id: this.dataid,
                            order_product_quantity: this.quantity,
                            order_subtotal: this.dataprice
                            });

正确的数据显示在控制台中,但在我的 JSON API 中,order_no 始终为零。除了order_no.,我发布的所有数据都有效。我该怎么做才能解决这个问题。希望您能够帮助我。提前谢谢你。

【问题讨论】:

  • 在第一个http post的subscribe功能中发送第二个http post吗?
  • 不,这只是我所做的。
  • 所以 this.ordernum 是未定义的,看我的回答

标签: javascript json angular angular2-http


【解决方案1】:

除非第一个 http post 被解析,否则 this.ordernum 没有定义,将第二个 http post 放在 subscribe 函数中即可:

 this.http.post('http://JSON-API',data,headers)
  .map(res => res.json())
  .subscribe(res => {
      console.log("Success Order No.: "+res);
      this.ordernum = res;
      let data2=JSON.stringify({
                        order_no: this.ordernum,
                        prod_id: this.dataid,
                            order_product_quantity: this.quantity,
                            order_subtotal: this.dataprice
                            });
      this.http.post('http://JSON-API',data2,headers)
      .map(response => response.json())
      .subscribe(response => {
          console.log("Order List No.: "+response);
          console.log(this.ordernum);
      });
  });

【讨论】:

  • 非常感谢!它对我有用。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2016-07-09
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
相关资源
最近更新 更多