javascript - Angular. How to chenge json response to jsonp response? -
i using openweathermap api json data, bu need , use jsonp data it, how in angular service ?
app.factory('forecast', ['$http', function($http) { this.sendapirequest = function(city){ return $http.get('http://api.openweathermap.org/data/2.5/forecast/city?q='+city+'&units=metric&mo') .success(function(data) { return data; }) .error(function(err) { return err; }); },
https://docs.angularjs.org/api/ng/service/$http#jsonp demonstrates proper way use angular receive jsonp objects.
the $http service call (from angulardocs) like:
$http({method: 'jsonp', url: $scope.url, cache: $templatecache}) .success(function(data, status) { $scope.status = status; $scope.data = data; })...
while markup binding functionality is:
<div ng-controller="fetchcontroller"> <select ng-model="method" aria-label="request method"> <option>get</option> <option>jsonp</option> </select> <input type="text" ng-model="url" size="80" aria-label="url" /> <button id="fetchbtn" ng-click="fetch()">fetch</button><br> <button id="samplegetbtn" ng-click="updatemodel('get', 'http-hello.html')">sample get</button> <button id="samplejsonpbtn" ng-click="updatemodel('jsonp', 'https://angularjs.org/greet.php?callback=json_callback&name=super%20hero')"> sample jsonp </button> <button id="invalidjsonpbtn" ng-click="updatemodel('jsonp', 'https://angularjs.org/doesntexist&callback=json_callback')"> invalid jsonp </button> <pre>http status code: {{status}}</pre> <pre>http response data: {{data}}</pre> </div>
so end result be:
app.factory('forecast', ['$http', function($http) { this.sendapirequest = function(city){ return $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/city?q='+city+'&units=metric&mo') .success(function(data) { return data; }) .error(function(err) { return err; }); },
as seen here: parsing jsonp $http.jsonp() response in angular.js
Comments
Post a Comment