Here is the URL, which in the browser renders as JSON:
;d=20&pt=PostalCode&format=json
Here is what I tried to store the data in a variable:
$http.get(';d=20&pt=PostalCode&format=json').then(function(response) {
$scope.zipCodes = response;
});
Here is the HTML where I tried to display it:
<pre>zipCodes {{zipCodes | json}}</pre>
But nothing displays, any idea what I'm doing wrong?
I also tried this:
$http.jsonp(';d=20&pt=PostalCode&format=json').then(function(response) {
$scope.zipCodes = response;
});
I've also tried AngularJS resource but that is also returning undefined:
var zipCodes = $resource(";d=20&pt=PostalCode&format=json",
{ callback: "JSON_CALLBACK" },
{ get: { method: "JSONP" }}
);
zipCodes.get({}, function(zipCode){
console.debug(zipCode.PostalCode);
});
console.debug(zipCodes.get());
$scope.zipCodes = zipCodes.get().results;
Here is the URL, which in the browser renders as JSON:
http://api.geosvc./rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode&format=json
Here is what I tried to store the data in a variable:
$http.get('http://api.geosvc./rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode&format=json').then(function(response) {
$scope.zipCodes = response;
});
Here is the HTML where I tried to display it:
<pre>zipCodes {{zipCodes | json}}</pre>
But nothing displays, any idea what I'm doing wrong?
I also tried this:
$http.jsonp('http://api.geosvc./rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode&format=json').then(function(response) {
$scope.zipCodes = response;
});
I've also tried AngularJS resource but that is also returning undefined:
var zipCodes = $resource("http://api.geosvc./rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode&format=json",
{ callback: "JSON_CALLBACK" },
{ get: { method: "JSONP" }}
);
zipCodes.get({}, function(zipCode){
console.debug(zipCode.PostalCode);
});
console.debug(zipCodes.get());
$scope.zipCodes = zipCodes.get().results;
Share
Improve this question
edited May 8, 2015 at 3:14
Jordash
asked May 7, 2015 at 21:51
JordashJordash
3,1038 gold badges45 silver badges80 bronze badges
1
-
1
$scope.zipCodes = response;
should be changed to$scope.zipCodes = response.data;
– Pankaj Parkar Commented May 7, 2015 at 22:22
2 Answers
Reset to default 3You need to use response.data
because while using .then
you get all 4 parameter inside response
object namely data
, status
, headers
, config
$scope.zipCodes = response.data;
Alternative
You could do use success
or error
function
$http.get('http://api.geosvc./rest/US/84606/nearby?apikey=485a35b6b9544134b70af52867292071&d=20&pt=PostalCode&format=json')
.success(function(data, status, headers, config) {
$scope.zipCodes = data;
})
.error(function(error, status, headers, config) {
console.log(status);
console.log("Error occured");
});
what is the filter json doing there?. if you hit the url what you get is the array of elements. Just try printing
<pre>zipCodes {{zipCodes}}</pre>
and then if you want to print anything else you can iterate over it using ng-repeat and display it as you need.