2017-01-27 3 views
0

に次のコードをAngularJSで書かれている、私はこのコードスニペット用の角度2と同等の構文を知りたいのですが:リクエストオプションのコールは、角2

$http({ 
     method: options.method, 
     url: 'api/url', 
     headers: options.headers, 
     data: options.data || {}, 
     params: options.params 
     }) 
     .then(success) 
     .catch(error); 
+0

がCORSに関連した本でありますか? –

答えて

0

あなたは.request() method of the HTTP serviceを使用することができます。

http.request(url, requestOptions) 
    .subscribe(success, error); 

export interface RequestOptionsArgs { 
    url?: string; 
    method?: string | RequestMethod; 
    search?: string | URLSearchParams; 
    headers?: Headers; 
    body?: any; 
    withCredentials?: boolean; 
    responseType?: ResponseContentType; 
} 
:URLが文字列であると requestOptionsfollowing shapeを持つオブジェクトです

...

optionsオブジェクトの構造によっては、構造体からrequestOptions構造体へのマッピングが必要な場合があります。

.request()メソッドはObservableを返します。 .subscribe()を呼び出し、successerrorのコールバックを渡します。

0

あなたのコードサンプルで動作することは、次のように使用することができます。

import {Http, Request, Response} from '@angular/http'; 
import 'rxjs/operator/add/toPromise'; 

...

$http(request: Request): Promise { 
    return this.http.request(request).toPromise(); 
}