gpt4 book ai didi

javascript - 使用 JSONP 从 Flickr api 获取 JSON 数据

转载 作者:行者123 更新时间:2023-12-03 00:56:41 25 4
gpt4 key购买 nike

我面临一个新问题 - 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

最佳答案

由于您使用的是 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 中进行subscribe,它将返回 Subscription 而不是 可观察

关于javascript - 使用 JSONP 从 Flickr api 获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52797533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com