angular2、ng2 http get post 传参

ng2 http获取服务器数据的时候传递参数详解

1、 六种方法获取服务器数据所传递的参数

get(url: string, options?: RequestOptionsArgs): Observable<Response>;
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>;
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>;
delete(url: string, options?: RequestOptionsArgs): Observable<Response>;
patch(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>;
head(url: string, options?: RequestOptionsArgs): Observable<Response>;

2、详解

this.http({
    url: "data/list.json" or "data/list.php" or ......
    method: "get",
    search: "name=zhangxuchao&password=heping" or 使用URLSearchParams() 对象
    headers: 使用Headers()对象设置 可选
    body: ""    可选
})

3、实例

import { Http, Response, URLSearchParams } from 'angular2/http';
let params = new URLSearchParams();
params.set('name', 'huge');
console.log(params.toString()) // name=huge
this.http.get(StaticSettings.BASE_URL, { search: params}).subscribe(
       
);
this.http.post(url, params).subscribe(res=> {
    console.log(res);
});

URLSearchParams说明地址:https://developer.mozilla.org…

    原文作者:HTTP
    原文地址: https://segmentfault.com/a/1190000007945217
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞