Angular 2个连续的http请求
在我的angular data服务中,我尝试发出两个http请求,第二个请求取决于第一个请求中的数据。第一个请求工作正常,但由于某种原因,第二个请求从未命中我的后端服务器。我希望有人能告诉我我做的是否正确,或者告诉我我做错了什么Angular 2个连续的http请求,angular,typescript,observable,angular-http,angular-observable,Angular,Typescript,Observable,Angular Http,Angular Observable,在我的angular data服务中,我尝试发出两个http请求,第二个请求取决于第一个请求中的数据。第一个请求工作正常,但由于某种原因,第二个请求从未命中我的后端服务器。我希望有人能告诉我我做的是否正确,或者告诉我我做错了什么 @Injectable() export class DataService { constructor( private http: Http ) { } public twoRequest() { this.http.get(`http://loca
@Injectable()
export class DataService {
constructor( private http: Http ) { }
public twoRequest() {
this.http.get(`http://localhost:3000/1st_request`).subscribe((data) =>
this.http.post(`http://localhost:3000/2nd_request`, {data: data}))
}
编辑:我没有订阅第二个请求。我不知道您必须订阅每个请求,即使它们位于同一代码块中您也需要
订阅http.post
。如果您不订阅它,它将永远不会执行请求
@Injectable()
export class DataService {
constructor( private http: Http ) { }
public twoRequest() {
this.http.get(`http://localhost:3000/1st_request`).subscribe((data) =>
this.http.post(`http://localhost:3000/2nd_request`, {data: data}).subscribe(/*...*/));
}
他不依赖花括号,他还没有订阅第二篇文章。由@surenPossible duplicate正确指出,它有点不同,因为我在同一个块中执行两个调用。我认为这个问题将来对其他人会有用。
public twoRequest() {
this.http.get(`http://localhost:3000/1st_request`).subscribe((data) => {
this.http.post(`http://localhost:3000/2nd_request`, {data:data}))
.subscribe((resp: any) => {
console.log(resp)
})
}
}