Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 2个连续的http请求_Angular_Typescript_Observable_Angular Http_Angular Observable - Fatal编程技术网

Angular 2个连续的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

在我的angular data服务中,我尝试发出两个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}))
}

编辑:我没有订阅第二个请求。我不知道您必须订阅每个请求,即使它们位于同一代码块中

您也需要
订阅
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)
             })
           }

    }