【发布时间】: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