Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

rxjs - how to make 2 dependent http request in Angular 2

i need to make 2 http request (the second dependent on the first) to insert user credentiels into my data base.

the first service get the user credentiels (http://localhost:55978/FleetViewDBWebService.asmx/ChekCreds?name=name1&subname=subname1) and check if the user already exist or not, return the 'ID' if it exist , or "ok" if the user doesn't exist.

then i need to subscribe to the first service and get the returned value. if "ok" call the second service (http://localhost:55978/FleetViewDBWebService.asmx/InsertUser?name=name1&subname=subname1&Telephone=334580021)
to insert the user credentiels , else return any message .

I call the first service and get the result but i have no idea how to add the second service.

is there any ideas

service.ts

CheckCreds(value: any) {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    let params = new URLSearchParams();



    params.set('name', (value.nom).toString());
    params.set('subname', (value.prenom).toString());

    return this.http.get(this.checkUri, {
        search: params, withCredentials: true
    })
        .map(res => {
            console.log("++++" + res.text());


            return JSON.parse(res.text());
        })
        .catch(this.handleError)


} 

component.ts

 submitForm($ev, value: any) {
    $ev.preventDefault();
    for (let c in this.valForm.controls) {
        this.valForm.controls[c].markAsTouched();
    }
    if (this.valForm.valid) {
        this.Service.CheckCreds(value)
            .subscribe(
            res => {
                string result=JSON.stringify(res);
            },
            e => {
                alert(e);
            },
            () => {
            }

            );

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The RxJS way would be to use the switchMap operator to wait for the response of the first request to arrive before firing the second request.

return this.http.get('url/1')
  .switchMap(res1 => {
    // use res1 to further control param of the second call
    this.http.get('url/2')
  })
  .subscribe(res2 => {
    //do stuff with the second response
  })

To do the requests in parallel (for requests which are not dependant of one another), use the forkJoin static operator.

return Observable.forkJoin(
  this.http.get('url/1'),
  this.http.get('url/2'),
)
  .subscribe(([res1, res2]) => {
    // res1 and res2 available after both requests are completed
  })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...