admin管理员组文章数量:1323714
I'm developing an AngularJS app, and I want to display some info from Wikipedia, but I can't access the data in Angular, because the CORS restriction.
$scope.wikipediaData = $http.get('.php?titles='+$scope.country.name.toLowerCase()+'&rawcontinue=true&action=query&format=json&prop=extracts');
It make the call and I see it in firebug, I copied the url and paste it in another tab and I can see the JSON response.
In firebug also I'm seeing that I have to activate CORS but I don't know how to do it, and I tried a lot of solutions that I have been reading in StackOverflow and another sites.
Anyone can help me?
Thanks
I'm developing an AngularJS app, and I want to display some info from Wikipedia, but I can't access the data in Angular, because the CORS restriction.
$scope.wikipediaData = $http.get('http://es.wikipedia/w/api.php?titles='+$scope.country.name.toLowerCase()+'&rawcontinue=true&action=query&format=json&prop=extracts');
It make the call and I see it in firebug, I copied the url and paste it in another tab and I can see the JSON response.
In firebug also I'm seeing that I have to activate CORS but I don't know how to do it, and I tried a lot of solutions that I have been reading in StackOverflow and another sites.
Anyone can help me?
Thanks
Share Improve this question edited Aug 24, 2015 at 14:07 Damjan Pavlica 34.1k10 gold badges75 silver badges78 bronze badges asked Apr 4, 2015 at 2:14 Juan Pablo Pola DemoroJuan Pablo Pola Demoro 1531 gold badge3 silver badges16 bronze badges1 Answer
Reset to default 12You could use JSONP to avoid the CORS problem.
In short: The server have to support JSONP because it will create a return string with your callback as a wrapper around the returned JSON. Then your callback function will get the JSON as parameter. You don't need to manage that because Angular will handle this for you.
You'll only have to add &callback=JSON_CALLBACK
to your url and use $http.jsonp
.
Please have a look at the demo below and here at jsfiddle.
(There is a return from wikipedia but I don't know if the return is what you want.)
var app = angular.module('myApp', []);
app.factory('wikiService', function($http) {
var wikiService = {
get: function(country) {
return $http.jsonp('http://es.wikipedia/w/api.php?titles=' + country.name.toLowerCase() + '&rawcontinue=true&action=query&format=json&prop=extracts&callback=JSON_CALLBACK');
}
};
return wikiService;
});
app.controller('MainController', function($scope, wikiService) {
wikiService.get({name: 'Germany'}).then(function(data) {
console.log(data);
$scope.wikiData = data.data;
});
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MainController">
<pre ng-bind="wikiData | json"></pre>
</div>
</div>
本文标签: javascriptAngularJS consume web service from Wikipedia APIStack Overflow
版权声明:本文标题:javascript - AngularJS: consume web service from Wikipedia API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126942a2421985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论