【问题标题】:Does Http get of Angular2 only support Json?Angular2的Http获取是否只支持Json?
【发布时间】:2017-02-19 03:29:13
【问题描述】:

ufc-api.service.ts

getFighters() {
    return this.http.get('http://ufc-data-api.ufc.com/api/v3/fighters')
                    .map(res => res.json());
}

fighter.component.ts

data: string;

constructor(private ufcApiService: UfcApiService) { }

clickGetFighter() {
  this.ufcApiService.getFighters()
  .subscribe(result => {
    this.data = JSON.stringify(result);
  },
  error => {
    this.data = error;
  },
  () => console.log('fighters done!!'));
}

它不起作用,我收到以下错误:

{ "_body": {}, "status": 0, "ok": false, "statusText": "", "headers": {}, "type": 3, "url": null }

如果我自己制作了 restful api,它就可以工作,但这是来自 UFC 数据 API 的网站。

有没有办法在 angular2 上使用 http get 获取 html 字符串,例如 .net 上的 HttpClient?

感谢您的帮助。

【问题讨论】:

    标签: json rest http angular


    【解决方案1】:

    您需要为您的应用程序提供标头。 在您的情况下,它将是文本/纯文本。

    【讨论】:

    • 如果我理解正确,你需要做res.text()而不是res.json()
    猜你喜欢
    • 2017-05-18
    • 2016-07-28
    • 2013-06-03
    • 2014-12-07
    • 2015-11-22
    • 2015-08-31
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多