Search code examples
javascriptjsonangularangular2-http

HTTP POST in Angular 2


I had a problem with posting data in 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);
  });

}

My problem is I cannot post the res data but when I use console log to it, it shows me the right data. What I did is I pass the res data to ordernum variable.

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

And then I am trying to POST it to the order_no to my JSON API.

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

The correct data shows in the console but in my JSON API, the order_no is always zero. All the data I POST works except for the order_no.. What should I do for me to solve this. Hope you can help me. Thank you in advance.


Solution

  • this.ordernum is not defined unless the first http post is resolved, put the second http post inside the subscribe function to have it:

     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);
          });
      });