Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2未发送HTTP GET_Http_Angular - Fatal编程技术网

Angular2未发送HTTP GET

Angular2未发送HTTP GET,http,angular,Http,Angular,调用test()时,(A)中列出的代码从不发送其HTTP请求,而(B)中几乎相同的代码可以工作。知道问题出在哪里吗?我通过查看服务器日志知道是否发送了请求。此外,如果我通过将请求粘贴到浏览器中来手动执行(A)中的请求,则会得到预期的响应。我被难住了 (A) 从“../toplevel/Constants”导入{Constants} 从“@angular/core”导入{Injectable}; 从'@angular/Http'导入{Http,Response,Headers,RequestOpt

调用
test()
时,(A)中列出的代码从不发送其HTTP请求,而(B)中几乎相同的代码可以工作。知道问题出在哪里吗?我通过查看服务器日志知道是否发送了请求。此外,如果我通过将请求粘贴到浏览器中来手动执行(A)中的请求,则会得到预期的响应。我被难住了

(A)

从“../toplevel/Constants”导入{Constants}
从“@angular/core”导入{Injectable};
从'@angular/Http'导入{Http,Response,Headers,RequestOptions};
从'rxjs/Rx'导入{Observable};
导入'rxjs/add/operator/map';
导入“rxjs/add/operator/catch”;
@可注射()
导出类签名服务{
构造函数(私有http:http,私有常量:常量){
}
getToken(用户名:string,密码:string):可观察{
var url=`${this.constants.apiRoot}/users/${username}?${password}`
log(`使用url${url}调用api服务器`)
返回此.http.get(url)
.map((res:Response)=>JSON.stringify(res.JSON()))
.catch((error:any)=>Observable.throw(error.json().error | |“服务器错误”);
}
测试(){
this.getToken('jc','yadayada')
}
}
(B:摘录)

getDocument(id:string):可观察{
返回this.http.get(`${this.apirl}/documents/${id}`)
.map((res:Response)=>新文档(res.json()['Document']))
.catch((error:any)=>Observable.throw(error.json().error | |“服务器错误”);
}

您的序列是冷的。您需要将测试更改为:

   this.getToken('jc', 'yadayada').subscribe()

要激活它并发送请求

,我确实看到了控制台发布的消息。日志来自(A)代码,您有错误代码还是什么?你能试着把
.subscribe()
添加到你的捕获旁边看看吗?你用过调试器吗;在你的功能中,一步一步地看发生了什么?
getDocument(id: string) : Observable<Document>{
  return this.http.get(`${this.apiUrl}/documents/${id}`)
  .map((res:Response) => new Document(res.json()['document']))
  .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

  }
   this.getToken('jc', 'yadayada').subscribe()