简体   繁体   English

Angular2 HTTP请求失败

[英]Angular2 HTTP request fails

This is my first question here. 这是我的第一个问题。 I have a problem with the http requests on an API. 我在API上的http请求有问题。 When I make the request, the following error shows up on the console: 当我发出请求时,控制台上会显示以下错误:

EXCEPTION: Cannot find a differ supporting object '[object Object]' of type 'object'. 例外:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor only supports binding to Iterables such as Arrays. NgFor仅支持绑定到Iterables,例如数组。

The content of each files is: 每个文件的内容是:

boot.ts 引导程序

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from "./app.component";
import {HTTP_PROVIDERS} from "angular2/http";

bootstrap(AppComponent,[HTTP_PROVIDERS]);

app.component.ts app.component.ts

import {Component} from 'angular2/core';
import {HTTPTestService} from "./services/peli.service";
import {Peli} from "./peli";

@Component({
    selector: 'my-app',
    template: `
        <h1>Búsqueda de película</h1>
        <input #titulo placeholder="Buscar...">
        <button class="btn btn-success"     (click)="infoPeli(titulo.value)">Buscar</button>
        <div *ngFor="#peli of pelis">
        <h1>{{peli.Title}}</h1>
        <p>{{peli.Year}}</p>
        </div>
    `,
    providers: [HTTPTestService]
})

export class AppComponent {

pelis:Peli[];

    constructor (private _httpService: HTTPTestService) {}

    infoPeli(nombre:string) {
        this._httpService.buscarPeli(nombre)
            .subscribe(
                data => this.pelis = data,
                error => alert(error),
                () => console.log(this.pelis)
            );
    }
}

peli.service.ts 维护服务

import {Injectable} from "angular2/core";
import {Http} from "angular2/http";
import 'rxjs/add/operator/map';

@Injectable()
export class HTTPTestService {
    constructor(private _http:Http) {
    }

    buscarPeli(nombre:string) {
        return this._http.get('http://www.omdbapi.com/?    t='+nombre+'&y=&plot=short&r=json').map(res => res.json());
    }
}

peli.ts 资质

export class Peli{
    Title:string;
    Year:string;
}

And the JSON file that I receive from the request (Input - spiderman): 以及我从请求中收到的JSON文件(输入-Spiderman):

{"Title":"Spiderman","Year":"1990","Rated":"N/A","Released":"N/A","Runtime":"5 min","Genre":"Short","Director":"Christian Davi","Writer":"N/A","Actors":"N/A","Plot":"N/A","Language":"German","Country":"Switzerland","Awards":"N/A","Poster":"N/A","Metascore":"N/A","imdbRating":"5.7","imdbVotes":"90","imdbID":"tt0100669","Type":"movie","Response":"True"}

I don't know where the problem is, everything seems to be OK... 我不知道问题出在哪里,一切似乎都还可以。

Thanks in advance!! 提前致谢!!

You have an <div *ngFor="#peli of pelis"> element. 您有一个<div *ngFor="#peli of pelis">元素。 And you are setting this.pelis to be an object here => 并且您将this.pelis设置为此处的对象=>

.subscribe(
                data => this.pelis = data,

And you are getting an 你得到一个

EXCEPTION: Cannot find a differ supporting object '[object Object]' of type 'object'. 例外:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor only supports binding to Iterables such as Arrays. NgFor仅支持绑定到Iterables,例如数组。

Since pelis is not an array you are getting this error. 由于pelis不是数组,因此会出现此错误。

For example: Instead of 例如:代替

data => this.pelis = data

Try: 尝试:

data => this.pelis = [data]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM