javascript AngularJS 从外部 URL 获取 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30112306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:42:53  来源:igfitidea点击:

AngularJS getting JSON data from external URL

javascriptjsonangularjs

提问by Jordash

Here is the URL, which in the browser renders as JSON:

这是 URL,它在浏览器中呈现为 JSON:

http://api.geosvc.com/rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode&format=json

http://api.geosvc.com/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.com/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:

这是我尝试显示的 HTML:

<pre>zipCodes {{zipCodes | json}}</pre>

But nothing displays, any idea what I'm doing wrong?

但没有任何显示,知道我做错了什么吗?

I also tried this:

我也试过这个:

$http.jsonp('http://api.geosvc.com/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:

我也试过 AngularJS 资源,但这也返回未定义:

var zipCodes = $resource("http://api.geosvc.com/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;

回答by Pankaj Parkar

You need to use response.databecause while using .thenyou get all 4 parameter inside responseobject namely data, status, headers, config

您需要使用,response.data因为在使用时.then您会获得response对象内的所有 4 个参数,即data, status, headers,config

$scope.zipCodes = response.data;

Alternative

选择

You could do use successor errorfunction

你可以做 usesuccesserrorfunction

$http.get('http://api.geosvc.com/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");
});

回答by chetank

what is the filter json doing there?. if you hit the url what you get is the array of elements. Just try printing

过滤器 json 在那里做什么?如果你点击 url,你得到的是元素数组。试试打印

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

然后如果你想打印任何其他东西,你可以使用 ng-repeat 迭代它并根据需要显示它。