如何在 AngularJS 资源中使用 JSONP [英] how to use JSONP in AngularJS resource
问题描述
我正在尝试将 json 对象导入到变量中.我根据教程使用服务.
我收到意外的令牌错误,因为我不应该使用 $scope.news = JsonSource.feed();
- 但我真的不知道我应该使用什么.我用谷歌搜索并搜索了 3 个小时,我只找到了 $http.或 $json.答案,但我觉得它可以更容易 - 更清晰.
(完美的解决方案是 $scope.news = JsonSource.feed().entries
;D
服务文件:
var AAAServices = angular.module('AAAServices', ['ngResource']);AAAServices.factory('JsonSource', ['$resource',功能($资源){返回 $resource('https://www.facebook.com/feeds/page.php', {}, {提要:{方法:'JSONP',{格式:'json',id:'459908',回调:JSON_CALLBACK},isArray:false}});}]);
控制器文件:
var AAAControllers = angular.module('AAAControllers', [])AAAControllers.controller('newsCtrl', ['$scope', 'JsonSource',函数 newsCtrl($scope, JsonSource) {$scope.news = JsonSource.feed();}]);
json 文件(几乎是;D)
<代码>{"title": "Tytuł","link": "https:\/\/www.facebook.com\/",条目":[{"title": "新闻 1",id":1"},{"title": "新闻2",id":2"}]}
已
我将 $resource('file.json 改为 https://www.facebook.com/feeds/page.php - 这样你就可以检查它是 json 还是 jsonp...
这就是那些认为(和我一样)如果某些东西可以在浏览器中运行它应该可以在服务器脚本中运行的人的答案.
- facebook 为墙上的内容提供了非常好的 json:
https://www.facebook.com/feeds/page.php?format=json&id=xxxx
- 但是你不能从 nodejs 获取它 - 因为跨域策略限制
更多信息在这里:加载 Facebook 墙提要 JSON 问题>
所以,现在我必须为 facebook 墙搜索 jsonp 流...唉....
I'm trying to import json object into variable. I use the services according to tutorial.
I receive unexpected token error, because i shouldn't use $scope.news = JsonSource.feed();
- but I really don't know what should I use. I googled and searched 3 hours I find only $http. or $json. answers, but I feel, that it could be done easier - clearer.
(The perfect solution would be $scope.news = JsonSource.feed().entries
;D
The services file:
var AAAServices = angular.module('AAAServices', [
'ngResource'
]);
AAAServices.factory('JsonSource', ['$resource',
function($resource) {
return $resource('https://www.facebook.com/feeds/page.php', {}, {
feed: {method:'JSONP', {format: 'json', id:'459908', callback : JSON_CALLBACK}, isArray:false}
});
}]);
The controllers file:
var AAAControllers = angular.module('AAAControllers', [])
AAAControllers.controller('newsCtrl', ['$scope', 'JsonSource',
function newsCtrl($scope, JsonSource) {
$scope.news = JsonSource.feed();
}]);
the json file (almost ;D)
{
"title": "Tytuł",
"link": "https:\/\/www.facebook.com\/",
"entries": [
{
"title": " news 1",
"id": "1"
},
{
"title": " news 2",
"id": "2"
}
]
}
Edited:
i change $resource('file.json into https://www.facebook.com/feeds/page.php - so you can check if it is json or jsonp...
This is the answer for those, who thinks (as me) that if something works in browser it should work in server scripts.
- facebook gives very nice json for the wall content:
https://www.facebook.com/feeds/page.php?format=json&id=xxxx
- But you can't get it from nodejs - because of cross domain policy-restriction
More about is here: Loading facebook wall feed JSON issues
So, now i have to search for jsonp stream for facebook wall... Sigh....
这篇关于如何在 AngularJS 资源中使用 JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!