javascript - Angular 2 中的 HTTP POST

标签 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.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 到我的 JSON API 的 order_no

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.,我发布的所有数据都有效。我该怎么做才能解决这个问题。希望你能帮我。提前谢谢你。

最佳答案

this.ordernum 没有定义,除非第一个 http post 被解析,将第二个 http post 放在订阅函数中以拥有它:

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

关于javascript - Angular 2 中的 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137842/

相关文章:

javascript - 向 Google Maps API 发出 GET 请求返回错误请求 400 错误 - javascript

xml - 如何解析 XML 代码并获取所需的属性和值

javascript - 选中/取消选中表格中的所有复选框

javascript - 使用vuejs显示多张图片

javascript - sencha app build production 以纯文本格式生成 910 个 JS 文件

css - 尝试将 typescript 上的 css 设置为 devextreme 组件时出错

typescript - 如何从 angular2 上的 setInterval 访问构造函数属性

javascript - 如何创建本地助手来整理 Vue 组件

javascript - 使用 Webpack 4.5 + Babel 6 + React 16 导入 JSON 时出错

json - JSON 到 VBA 转换过程的对象所需错误