Javascript Angular 2中的HTTP POST

Javascript Angular 2中的HTTP POST,javascript,json,angular,angular2-http,Javascript,Json,Angular,Angular2 Http,我在angular 2中发布数据时遇到问题 postOrder() { let headers = new Headers(); headers.append('Content-Type', 'application/json'); let data=JSON.stringify({ customer_id: this.cusid, mode_code: this.

我在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;
      });
然后我尝试将它发布到我的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\u no
始终为零。我发布的所有数据都有效,但订单号除外。。我该怎么做才能解决这个问题。希望你能帮助我。提前感谢。

除非解析了第一个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);
      });
  });

您是否在第一个http post中的subscribe函数中发送第二个http post?不,这只是我所做的。因此,this.ordernum未定义,请参阅我的回答。非常感谢!这对我有用。非常感谢你。