【问题标题】:Getting JSON data using JSONP from Flickr api从 Flickr api 使用 JSONP 获取 JSON 数据
【发布时间】:2019-03-18 17:57:06
【问题描述】:

我面临一个新问题 - Jsonp。我已经阅读并观看了视频,但无法找到解决问题的方法。

首先,我使用的是 Angular 6。我试图从使用 JSONP 的 api 获取 json 响应,但是当我尝试在我的应用程序中使用它时,出现了 CORS 错误。所以我想修复它而无需安装 chrome 的 CORS 插件。浏览器上的响应是这样的:

jsonFlickrFeed({
  "title": "Uploads from everyone",
  "link": "https:\/\/www.flickr.com\/photos\/",
  "description": "",
  "modified": "2018-10-13T21:16:50Z",
  "generator": "https:\/\/www.flickr.com",
  "items": [{
      "title": "photo 2504 was taken at 14:18:38",
      "link": "https:\/\/www.flickr.com\/photos\/barrycorneliusox\/30359327537\/",
      "media": {
        "m": "https:\/\/farm2.staticflickr.com\/1944\/30359327537_fac974807d_m.jpg"
      },
      "date_taken": "2018-10-13T14:18:38-08:00",
      "description": " <p><a href=\"https:\/\/www.flickr.com\/people\/barrycorneliusox\/\">barrycorneliusox<\/a> posted a photo:<\/p> <p><a href=\"https:\/\/www.flickr.com\/photos\/barrycorneliusox\/30359327537\/\" title=\"photo 2504 was taken at 14:18:38\"><img src=\"https:\/\/farm2.staticflickr.com\/1944\/30359327537_fac974807d_m.jpg\" width=\"240\" height=\"160\" alt=\"photo 2504 was taken at 14:18:38\" \/><\/a><\/p> <p><a href=\"http:\/\/www.oxonraces.com\/photos\/2018-10-13-oxford\/\" rel=\"nofollow\">Click here for more photos of the 2018 Chiltern XC League Match 1 - Oxford<\/a>. If you put a photo somewhere public, please add the credit <em>Photo by Barry Cornelius<\/em>.<\/p>",
      "published": "2018-10-13T21:16:50Z",
      "author": "nobody@flickr.com (\"barrycorneliusox\")",
      "author_id": "159968055@N03",
      "tags": "2018 chilternxcleaguematch1oxford oxford jsvmenpointjfirstlap barrycornelius oxonraces"
    },
    .....,
  ]

})

我的服务:(使用 jsonp)

getImages() {
  return this.jsonp.request(this.imagesUrl).subscribe(res => {
    console.log(res);
  })
}

不起作用。我做错了什么?

我的 api 网址:

https://api.flickr.com/services/feeds/photos_public.gne?format=json

【问题讨论】:

  • 我假设您已经导入了 JsonpModule。另外,您还在 url 末尾添加了 callback=JSONP_CALLBACK?
  • 我现在收到此错误:未捕获的 ReferenceError: jsonFlickrFeed 未定义@r2018
  • 你能不能创建stackblitz,然后我可以看看

标签: javascript angular typescript jsonp


【解决方案1】:

由于您使用的是 Angular 6,

您必须导入 HttpClientModule, HttpClientJsonpModule 并将它们添加到您要进行此调用的模块的 imports 数组中:

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

@NgModule({
  imports: [..., HttpClientJsonpModule, HttpClientModule, ...],
  ...
})
export class AppModule { }

然后在您的服务中,在 API URL 中,您还必须指定 jsoncallback=JSONP_CALLBACK 而不是 nojsoncallback=callback

import { Injectable } from '@angular/core';

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

@Injectable()
export class PhotoService {

  imagesUrl = `https://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=JSONP_CALLBACK`;

  constructor(private http: HttpClient) { }

  getImages() {
    return  this.http.jsonp(this.imagesUrl, 'JSONP_CALLBACK');
  }

}

然后在你的组件中:

...

import { PhotoService } from './photo.service';

...
export class PhotosComponent  {

  myArray: any[];

  constructor(private photoService: PhotoService) { 
  }

  ngOnInit() {
    this.photoService.getImages()
      .subscribe((res: any) => this.myArray = res.items);
  }

}

这里有一个Sample StackBlitz 供您参考。

PS:您也在 getImages 内部 subscribeing,这将返回 Subscription 而不是 Observable

【讨论】:

  • api.flickr.com/services/feeds/photos_public.gne?format=json@SiddAjmera 使用此 url 错误是:jsonFlickrFeed 未定义
  • 它工作得很好,谢谢。关于它的最后一个问题:为什么这给了我关于“项目”的错误:this.dataService.getImages().subscribe(res => { this.myArray = res.items; @SiddAjmera
  • 它在我的 IDE 中工作会引发错误:对象 @SiddAjmera 类型上不存在属性项
  • 视觉工作室代码@SiddAjmera。在你的 stackblitz 上我也看到了错误..
猜你喜欢
  • 2019-07-18
  • 1970-01-01
  • 2015-12-20
  • 2013-09-26
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 2012-08-18
相关资源
最近更新 更多