简体   繁体   English

从Flickr API使用JSONP获取JSON数据

[英]Getting JSON data using JSONP from Flickr api

I'm facing a new issue - Jsonp. 我正面临一个新问题-Jsonp。 I've read about and watched videos but couldn't get to the solution to fix my problem. 我已阅读并观看了视频,但无法解决该问题。

First I'm using Angular 6. I'm trying to get a json response from an api which uses JSONP, but when I try to use it in my app I got CORS error. 首先,我使用Angular6。我试图从使用JSONP的api获取json响应,但是当我尝试在我的应用程序中使用它时,出现CORS错误。 So I want to fix it without having to install chrome's CORS plugin. 因此,我想修复它而不必安装chrome的CORS插件。 The response on the browser is like this: 浏览器上的响应是这样的:

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"
    },
    .....,
  ]

})

my service:(using jsonp) 我的服务:(使用jsonp)

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

doesn't work. 不起作用。 what am I doing wrong? 我究竟做错了什么?

my api URL : 我的api URL:

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

Since you're using Angular 6, 由于您使用的是Angular 6,

You'll have to import HttpClientModule, HttpClientJsonpModule and add them to your imports array of your Module that you want to make this call in: 您必须导入HttpClientModule, HttpClientJsonpModule并将它们添加到要在其中进行此调用的模块的imports数组中:

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

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

Then in your service, in the API URL, you'll also have to specify jsoncallback=JSONP_CALLBACK instead of nojsoncallback=callback : 然后在服务中的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');
  }

}

And then in your Component: 然后在您的组件中:

...

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);
  }

}

Here's a Sample StackBlitz for your ref. 这是供您参考的示例StackBlitz

PS: You're also subscribe ing inside getImages which would return a Subscription instead of an Observable PS:你还subscribe荷兰国际集团内部getImages这将返回一个Subscription ,而不是一个的Observable

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

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