我在 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/