带有Id的Http获取请求在Angular 2.3.1中结束

时间:2018-05-28 08:20:20

标签: angular http

我尝试将以下代码用于带有user_id的get请求,但它不起作用。

getProjects(user_id){
this.loadToken();
let headers = new Headers();
headers.append('Authorization', this.authToken);
headers.append('Content-Type','application/json');
let params = new URLSearchParams();    
params.set('user_id', user_id);
return this.http.get('http://localhost:3000/project/all/' , {headers: headers, search: params})
  .map(res => res.json());

}

当我在get请求中放置完整路径而不尝试params时,它返回正常。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您需要使用RequestOptions,如下所示:

getProjects(user_id){
  this.loadToken();

  let headers = new Headers();
  headers.append('Authorization', this.authToken);
  headers.append('Content-Type','application/json');

  let params: URLSearchParams = new URLSearchParams();
  params.set('user_id', user_id);

  let requestOptions = new RequestOptions();
  requestOptions.search = params;

  this.http.get('http://localhost:3000/project/all/', requestOptions)
    .map(res => res.json());
}