简体   繁体   English

使用 Angular 从 API 获取 JSON 数据

[英]Get JSON data from API with Angular

I am trying to get some JSON data by the following address http://www.soe.gr/photos.txt which is created with Flickr API by using the following Angular code:我试图通过以下地址http://www.soe.gr/photos.txt获取一些 JSON 数据,该地址是通过使用以下 Angular 代码使用Flickr API创建的:

angular.module('myapp', ['ngResource']).
controller('MyController', ['$scope', '$resource', function ($scope,$resource) {
    $scope.photoAPI = $resource("http://www.soe.gr/photos.txt",
{ callback: "JSON_CALLBACK" }, { get: { method: "JSONP" }});
$scope.photos=$scope.photoAPI.get();
}]);

I am using $resource as not to be blocked since I am trying to get data from another domain.我正在使用$resource作为不被阻止,因为我试图从另一个域获取数据。 However, I am getting the following error: Uncaught ReferenceError: jsonFlickrFeed is not defined .但是,我收到以下错误: Uncaught ReferenceError: jsonFlickrFeed is not defined

  • My first question is what exactly is this kind of typing: jsonFlickrFeed({...})我的第一个问题是这种类型到底是什么: jsonFlickrFeed({...})
  • My second question is, of course, how may I get the items property of the above JSON object into my $scope as to manipulate them and proceed with my code since I am stuck!我的第二个问题当然是,如何将上述 JSON 对象的items属性放入我的 $scope 中,以便操作它们并继续我的代码,因为我被卡住了! Thank you谢谢

$resource is for handling REST APIs even if its possible to redefine get method with jsonp it has no use, its better to do it using $http : $resource 用于处理 REST API,即使它可能使用 jsonp 重新定义 get 方法它没有用,最好使用 $http 来做到这一点:

  var url = "http://www.soe.gr/photos.txt?callback=JSON_CALLBACK";
  $http.jsonp(url).then(function(response) {
    console.log(response.data);
  });

What is JSONP: https://en.wikipedia.org/wiki/JSONP什么是 JSONP: https : //en.wikipedia.org/wiki/JSONP

JSONP (JSON with Padding) is a technique used by web developers to overcome the cross-domain restrictions imposed by browsers to allow data to be retrieved from systems other than the one the page was served by. JSONP(带填充的 JSON)是 Web 开发人员用来克服浏览器强加的跨域限制的一种技术,以允许从提供页面的系统以外的系统检索数据。

Its main point its not loading JSON data but load the script where you can see function call with json data as parameter - in your file you can see jsonFlickrFeed({...}).它的主要观点不是加载 JSON 数据,而是加载脚本,您可以在其中看到使用 json 数据作为参数的函数调用 - 在您的文件中,您可以看到 jsonFlickrFeed({...})。 But the function needs to exist.但该功能需要存在。 In AngularJS application angular provides it for you transparently and sends its generated name to the requested script instead of JSON_CALLBACK in parameter.在 AngularJS 应用程序中,angular 透明地为您提供它并将其生成的名称发送到请求的脚本而不是参数中的 JSON_CALLBACK。

But here your endpoint is probably just static file not script which could provide JSONP response with given customized callback function.但是在这里您的端点可能只是静态文件而不是脚本,它可以通过给定的自定义回调函数提供 JSONP 响应。

You can see its working here - http://jsbin.com/becufenepo/edit?js,console你可以在这里看到它的工作 - http://jsbin.com/becufenepo/edit?js,console

Its url contains parameter jsoncallback=JSON_CALLBACK, angular sends generated name and in network console you can see request for它的 url 包含参数 jsoncallback=JSON_CALLBACK,angular 发送生成的名称,在网络控制台中你可以看到请求

https://www.flickr.com/services/rest/ ... &jsoncallback=angular.callbacks._0

And its response then contains function calling angular.callbacks._0({})然后它的响应包含调用angular.callbacks._0({})函数

Thats all, so main cause is that your endpoind its not capable to provide jsonp.就是这样,主要原因是您的终端无法提供 jsonp。

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

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