角2连续http请求

角2连续http请求

问题描述:

在我的角度数据服务中,我试图做出两个http请求,第二个请求取决于来自第一个请求的数据。第一个请求工作正常,但由于某种原因,第二个请求永远不会打到我的后端服务器。我希望如果有人能够告诉我,如果我正确地做到了这一点,或者告诉我我做错了什么。角2连续http请求

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

编辑:我没有订阅第二个请求。我不知道你必须订阅你所做的每一个请求,即使他们在同一个代码块中也是如此

+0

的可能的复制[2角HTTP GET没有得到(https://*.com/questions/41381200/angular-2-http-get-not-getting) – Alex

+0

它有点不同因为我正在同一个块中进行两个调用。我认为这个问题对未来的其他人有用。 – xeroshogun

你也需要subscribehttp.post也。如果您不要求subscribe,它永远不会提出要求。

@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(/*...*/)); 
} 

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

    } 
+0

没有大括号的依赖,他没有订阅第二篇文章。正确指出@suren –