Angular 4 api 调用转向对 json 的响应返回未定义

Bai*_*ler 4 json angular

我正在关注一个示例,他们正在像这样调用本地 web api。

return this.http.get("http://localhost:26264/api/news").map((response: Response)=>{
  response.json();
});
Run Code Online (Sandbox Code Playgroud)

如果您在.json()通话前查看response 的值,一切看起来都很好。

然后我在做这个,

var data = this.authService.Login(value.Email, value.Password).subscribe((res: any)=>{
    console.log(res);
  });
Run Code Online (Sandbox Code Playgroud)

此时 res 的值未定义?忽略我正在为新闻控制器 api 调用登录方法这一事实。我更改了 api 端点,因为在此之前我遇到了其他错误。

Mos*_*she 5

考虑改变:

.map((response: Response)=>{
  response.json();
});
Run Code Online (Sandbox Code Playgroud)

.map((response: Response) => response.json())
Run Code Online (Sandbox Code Playgroud)

这样做的原因是..

.map(response: Response => response.json() )
Run Code Online (Sandbox Code Playgroud)

说创建一个匿名函数并接收一个响应对象并从响应对象返回 json 方法(它返回序列化为 JSON 的对象)

.map((response: Response)=>{
  response.json();
});
Run Code Online (Sandbox Code Playgroud)

说创建一个接受响应对象的匿名函数,并在此匿名函数的范围内,从响应对象运行 json 方法,该方法不返回任何内容。

修理:

.map((response: Response)=>{
  return response.json();
});
Run Code Online (Sandbox Code Playgroud)