2017-06-25 28 views
0

投稿要求が正常に終了した後に関数を呼び出したいとします。それを達成する方法?要求が成功した後にアクションを実行します。

これは私のポスト要求である:

this.http.post(this.myUrl, formData, options) 
    .map(res => res.json()) 
    .catch(error => Observable.throw(error)) 
    .subscribe(
    data => console.log('success'), 
    error => console.log(error) 
) 

答えて

0

あなたが好きなだけのコードを追加することができ、あなたのコールバックで{}を使用することにより。

this.http.post(this.myUrl, formData, options) 
    .map(res => res.json()) 
    .catch(error => Observable.throw(error)) 
    .subscribe(
    data => { 
     // you can do everything you want here 
     console.log('success'); 
     this.myCustomFunction(); 
    }, 
    error => console.log(error) 
) 
+0

これにより、コンソールで「SyntaxError:JSON入力の予期しない終了」エラーが発生します。 – user4824732

+0

私は二重チェックし、構文が正しいです。 Typescriptでコーディングしていますか? – Ploppy

+0

私はその応答が実際にはJSON文字列ではないと思われますので、例外は 'res.json()'行によって発生します。 – GregL

0
this.http.post(this.myUrl, formData, options) 
    .map(res => res.json()) 
    .subscribe(
    (data) => { 
     console.log('success'); 
     this.myFunction(); //call any function 
    }, 
    (error) => { 
     console.log(error) 
    }); 

http.post(...)。マップ(...)あなたが後に機能を起動したい場合は、それがプロセス非同期で観察可能、.subscribe(...)を返しますhttpレスポンスでは、関数内でサブスクライブする必要があります。

(data) => { // inside invoke the function that you want} 
+0

最初の「}」文字の後にカンマがあってはいけませんか?しかし、原因は "SyntaxError:JSON入力の予期しない終了"エラーです。 – user4824732

+0

はい、そうです、修正されました – alehn96

関連する問題