angular2、ng2 http get post 传参

前端之家收集整理的这篇文章主要介绍了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,options?: RequestOptionsArgs): Observable<Response>;
delete(url: string,options?: RequestOptionsArgs): Observable<Response>;
patch(url: 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...

猜你在找的Angularjs相关文章