如何使用Angular为Http get请求添加id到url

时间:2018-05-29 06:48:03

标签: angular http

我在angular2服务中有一个函数,它传递一个id以附加到Http get请求的基本URL但是它不起作用。当我输入确切的字符串时,它可以很好地获取数据,但它不会让我附加变量,如

' http://localhost:3000/project/allTask/' + project_id

dec esi

更新

我相信未定义时传递的project_id。这是我的组件的功能。

getTasks(project_id){
  this.loadToken();
  let headers = new Headers();
  headers.append('Content-Type','application/json');
  return this.http.get('http://localhost:3000/project/allTask/5b0919ea256f2e15f8f4364f' , {headers: headers})
    .map(res => res.json());
}

1 个答案:

答案 0 :(得分:1)

就这样使用它:

getTasks(project_id){
  this.loadToken();
  let headers = new Headers();
  headers.append('Content-Type','application/json');
  return this.http.get('http://localhost:3000/project/allTask/' + project_id, {headers: headers})
    .map(res => res.json());
}

getTasks(project_id){
  this.loadToken();
  let headers = new Headers();
  headers.append('Content-Type','application/json');
  return this.http.get(`http://localhost:3000/project/allTask/${project_id}`, {headers: headers})
    .map(res => res.json());
}
相关问题