最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS getting JSON data from external URL - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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.

发布评论

评论列表(0)

  1. 暂无评论