使用angular 2在同一函数中调用GET和POST服务调用

时间:2016-05-18 03:15:50

标签: angular

如果响应成功,如何在GET之后调用http POST服务调用。

我按以下方式编写了GET调用,

...

POST的示例网址:

  

https://itunes.apple.com/1/json

import {Page, NavController} from 'ionic-angular';
import {Http, Headers} from 'angular2/http';
import 'rxjs/Rx';

@Page({
  templateUrl: 'build/pages/login/login.html'
})


export class LoginPage {    
  constructor(private nav: NavController, public http: Http) {
  }

  onLogin(value: string): void { 
    if(this.authForm.valid) {        
      this.http.get('https://itunes.apple.com/us/rss/topmovies/limit=1/json')
      .map(res => res.json())
      .subscribe(
        data => {console.log(JSON.stringify(data));},
        err => this.logError(err),
        () => console.log('Random Quote Complete')
      );

      this.nav.push(AccountViewPage);
    }
  } 

  logError(err) {
    console.error('There was an error: ' + err);
  }
}

1 个答案:

答案 0 :(得分:1)

 **It could be better if you use another Function sucees**  

  onLogin(value: string): void { 
   if(this.authForm.valid) {        
     this.http.get('https://itunes.apple.com/us/rss/topmovies/limit=1/json')
     .map(res => res.json())
      .subscribe(
       data => {console.log(JSON.stringify(data));},
       err => this.logError(err),
       () => this.add();
      );

     this.nav.push(AccountViewPage);
  }
  } 

  public add(): void {
  // another service here
  }