使用Observable的HTTP调用不起作用 [英] HTTP call using Observable not working

查看:117
本文介绍了使用Observable的HTTP调用不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的函数中,我有两个对服务器的调用,一个使用Observable,另一个使用Promise.使用Observable的呼叫无法到达服务器,但是使用promise的呼叫可以到达服务器.知道为什么吗?

In the function below, I have two calls to the server, one using Observable and the other using Promise. The call using Observable does not reach the server but the one using promise does. Any idea why?

public placeOrder(order:string) {

//Using Observable
this.http.post(this.newOrderUrl, {order: order}, this.options)
.map((response:Response) => {
  console.log('new order', response.json())
})

//Using Promise
this.http.post(this.newOrderUrl, {order: order}, this.options)
.toPromise()
.then((response:Response) => {
  console.log('new order', response.json())
})
}

推荐答案

如果您使用的是Observable,则需要返回response.json()

You need to return the response.json() if you are using Observable

 return this.http.post(this.newOrderUrl, {order: order}, this.options)
   .map((response: Response) => response.json()
 );

并在您的组件中,使用subscription()调用

and in your component, call using subscribe()

this._myservice.placeOrder('somestring').subscribe((orders: any) => {
});

这篇关于使用Observable的HTTP调用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆