2016-05-18 76 views
0

如果响应成功,我如何在GET后调用http POST服务调用。使用角度2在同一函数中调用GET和POST服务调用

我已经书面按照以下方式get调用,

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

为POST样品网址:

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

headers: {"Content-Type": "application/x-www-form-urlencoded" }, 
ignoreAuthModule: 'ignoreAuthModule' 
+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 
    } 
+0

谢谢@ mayur..that是我的期望..但它不会执行第二个功能,直到第一个响应正确? – vishnu

+0

你可以在数据后调用add()或之后this()=>,因为它依赖于你想要的成功 – mayur

+0

谢谢@ mayur ..更多我越来越错误,同时传递参数到GET调用;类型'{params:URLSearchParams; }'不能分配给'RequestOptionsArgs'类型的参数。这是我的代码:let options = new RequestOptions({params:new URLSearchParams('validateUsr = false') }); – vishnu