2017-01-27 46 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); 

...这里的URL是一个字符串,requestOptionsfollowing shape对象:

export interface RequestOptionsArgs { 
    url?: string; 
    method?: string | RequestMethod; 
    search?: string | URLSearchParams; 
    headers?: Headers; 
    body?: any; 
    withCredentials?: boolean; 
    responseType?: ResponseContentType; 
} 

根据您的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(); 
}