Angular2:res.json不是函数

时间:2016-08-26 08:43:52

标签: angular

我正在尝试在我的代码中使用以下服务:

import {Injectable} from '@angular/core';
import {Http,Response} from "@angular/http";
import 'rxjs/Rx';

@Injectable()
export class HttpService{
    constructor(private http : Http){}

    getData(){
      return  this.http.get("URI")
      .map( (res: Response) => res.json()  );
    }
}

问题是,在运行时它抱怨:

res.json is not a function

我已将res的数据类型定义为Response,但仍抱怨

.map( (res: Response) => res.json()  ) 

如果我用subscribe替换地图它可以正常工作:

.subscribe( res =>{
                res.json();
                console.log("City is:"+ res.json().city.name)
            });

5 个答案:

答案 0 :(得分:18)

为了它的价值,在Angular 5中我发现如果响应是纯json,只需返回res.json is not a function即可修复res错误。

答案 1 :(得分:6)

尝试var myPromise = myService.postMyData(data). myPromise.then(function(){//success },function(){//error },function(){//inform that action is in progress }) 代替import 'rxjs/add/operator/map';。这应该可以解决问题。

答案 2 :(得分:0)

您不需要使用此功能: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" /> <div class="modal" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"></div> <div class="modal-body"> <p>The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"</p> </div> <div class="modal-footer"> <div class="comment-input"> <div class="textarea" contenteditable="true"></div> </div> </div> </div> </div> </div> 要么 .map((res: Response) => res.json() );是因为.map(res => res.json());自动提供或应用于HttpClient.get()并返回res.json()

您不再需要再次调用此函数。只是简单地这样说:Observable<HttpResponse<string>>这可以解决或解决问题。

答案 3 :(得分:0)

如果您的响应已经是Json,则无需致电res.json(),您可以直接与res合作,在这种情况下甚至不需要使用map

  getData(){
  return  this.http.get("URI");
}

答案 4 :(得分:-1)

尽量只放这样

.map((response: Response) => response)

这项工作肯定在Angular