【问题标题】:Angular2 Flickr JSONPAngular2 Flickr JSONP
【发布时间】:2017-11-22 02:48:57
【问题描述】:

我对 AngularJS 很陌生。我正在使用 Angular2 制作 Flickr 公共提要应用程序,但遇到了这个问题。

我找不到让 JSONP 工作的方法。我应该怎么做才能返回 JSON 对象?

这是代码

flickr.service.ts

import { Injectable } from '@angular/core';
import { Jsonp, Http } from '@angular/http'; //use jsonp module to fetch the data from the api call

import 'rxjs/Rx'; // use rxjs as the observable

@Injectable()

export class FlickrService {

  url: string;
  apiKey: string = 'ffed2ef9dede27481c3195eea1be61eb';

  constructor(private _jsonp: Jsonp) {
    this.url = `https://api.flickr.com/services/feeds/photos_public.gne?&api_key=${this.apiKey}&per_page=12&format=json&nojsoncallback=1`;
  }

  getFeed() {
    return this._jsonp.get(this.url)
      .map(res => res.json());
  }

  searchTag(searchTerm: string) {
    return this._jsonp.get(this.url+'&tag='+searchTerm)
      .map(res => res.json());
  }
}

flickr.component.ts

import { Component } from '@angular/core';
import { FlickrService } from '../services/flickr.service';
@Component({
  moduleId: module.id,
  selector: 'flickr',
  templateUrl: './flickr.component.html'
})
export class FlickrComponent {
  feedList: Array<Object>;
  searchList: Array<Object>;
  searchTerm: string;
  constructor(private _flickrService: FlickrService) {
    this._flickrService.getFeed().subscribe(res => {
      console.log(res);
    });
  }
}

【问题讨论】:

  • 可以添加ngmodule代码吗?
  • 你知道如何解决这个问题吗?我也面临同样的问题。

标签: angular jsonp


【解决方案1】:

对于任何像我一样努力让它工作的人,我终于让它在 Angular 5 中以下列方式工作:

在 app.module.ts 中添加 HttpClientJsonpModule

import {HttpClientModule, HttpClientJsonpModule, HttpClient} from '@angular/common/http';

imports: [
    ...

    HttpClientModule,
    HttpClientJsonpModule
...
]

在我拥有的 api 服务中:

getFlickrFeed() {
    const url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=json&id=YOUR_ID&jsoncallback=JSONP_CALLBACK';

    return this.http.jsonp(url, 'JSONP_CALLBACK').pipe(
            tap(
                data => data
            )
        );
}

在我的模块中:

    this.api.getFlickrFeed().subscribe(
        data => {

            this.flickrFeed = data['items'];

            console.log(this.flickrFeed);
        }
    );

【讨论】:

  • 非常好,感谢您发布此内容。但是我必须从这段代码中删除 tap() 才能使其工作。
  • 很高兴它有帮助。顺便说一句,您是否导入了水龙头?从 'rxjs/operators' 导入 {tap};
【解决方案2】:

为了让Jsonp 库为我工作,我需要以callback=JSONP_CALLBACK 结束我的查询。因此,请尝试将您的服务更新为此...

import { Injectable } from '@angular/core';
import { Jsonp, Http } from '@angular/http';

import 'rxjs/Rx'; // use rxjs as the observable

@Injectable()

export class FlickrService {

  url: string;
  apiKey: string = 'ffed2ef9dede27481c3195eea1be61eb';

  constructor(private _jsonp: Jsonp) {
    this.url = `https://api.flickr.com/services/feeds/photos_public.gne?&api_key=${this.apiKey}&per_page=12&format=json`;
  }

  getFeed() {
    return this._jsonp.get(this.url+'&callback=JSONP_CALLBACK')
      .map(res => res.json()).catch(this._handleError);
  }

  searchTag(searchTerm: string) {
    return this._jsonp.get(this.url+'&tag='+searchTerm+'&callback=JSONP_CALLBACK')
      .map(res => res.json()).catch(this._handleError);
  }

  private _handleError(error: Response | any) {
    console.log(error);
    return Observable.throw(error.code);
  }
 }

另外,我不能 100% 确定 callback=JSONP_CALLBACK 需要 是否是查询的最后一个参数......但是我读过的任何和所有文档都在那里,所以我刚刚做到了,它对我来说效果很好。

【讨论】:

  • 我试过了,没用 :( 控制台显示错误。jsonFlickrFeed 未定义。
  • jsonFlickrFeed 不是一个函数......就像,我不知道它来自哪里!您介意附上屏幕截图或完整的错误日志吗?
  • 更新我的答案以有一个错误处理程序,如果可能的话,我也很想从那里查看日志。
  • 等一下,您是在将JsonpModule 导入到您的应用模块中吗? (你需要这样做!)
  • 是的,我在app.module.ts 中做过。该错误似乎与以前相同。 imgur.com/a/2ozuV
猜你喜欢
  • 2019-07-18
  • 2015-12-20
  • 2012-05-09
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多