首页 文章

无法将响应映射到对象

提问于
浏览
0

我正在尝试将Angular4 HttpClient与我定义的可观察对象一起使用 . 不幸的是,我似乎无法将响应映射到对象 .

问题似乎是我正在使用httpclient(它隐式返回json,所以没有response.json()函数),据我所知,http被弃用了?无论如何因为这个,response.json()会导致错误;错误TypeError:response.json不是函数

代码;

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';

import {Observable} from 'rxjs/Observable';
import {JsonpModule, Jsonp, Response} from '@angular/http';

export class BucketList {
  constructor(public name: string,
              public creationdate: string) {
  }
}

@Injectable()
export class DocumentSearchService {

    constructor(private _http : HttpClient) { }

    getBucketList () : Observable<BucketList> {

      let serviceURL = "http://localhost:3000/listBuckets";

      return this._http.get(serviceURL, {withCredentials: true, responseType: 'json'})

      .map((response: Response) => <BucketList>(response.json())
      .catch((error: any) => window.console.log(error)));
    }
}

ngOnInit() {
// this.BucketList = this.DocumentSearchService.getBucketList

   this.BucketList = 
   this.DocumentSearchService.getBucketList().subscribe(value => {
}

有人能指出我正确的方向吗?谷歌搜索和搜索到目前为止没有答案...

谢谢 .

1 回答

  • 0

    responseType: 'json'response.json() 可以省略,因为它们是隐含的by default

    responseType值确定如何解析成功的响应正文 . 如果responseType是默认的json,则结果对象的类型接口可以作为类型参数传递给request() . <...> get <T>(url:string,options:{
    Headers ?:HttpHeaders,
    观察:'事件',
    params?:HttpParams,
    reportProgress?:boolean,
    responseType?:'json',
    withCredentials ?:布尔,
    }):Observable <HttpEvent <T >>
    构造一个GET请求,该请求将主体解释为JSON并返回完整的事件流 .

相关问题