2016-02-01 131 views
1

我创建控制器和html页面我想用JSON字符串获取http响应但是我什么也没看到。获取角度HTTP GET的JSON响应

angular.module('myApp.view3', ['ngRoute']) 

    .config(['$routeProvider', function($routeProvider) { 
     $routeProvider.when('/view3', { 
      templateUrl: 'view3/view3.html', 
      controller: 'View3Ctrl' 
     }); 
    }]) 

    .controller('View3Ctrl',function($scope, $http) { 
     $scope.my_name = "Pasha"; 
     $http({ 
      method : "GET", 
      url : "http://api.geosvc.com/rest/US/84606/nearby?apikey=#APIKEY&d=20&pt=PostalCode&format=json" 
     }).then(function mySucces(response) { 
      $scope.myWelcome = response.data; 
     }, function myError(response) { 
      $scope.myWelcome = response.statusText; 
     }); 
    }); 

这是我的html页面

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>My view</title> 
</head> 
<body ng-controller="View3Ctrl"> 
<p> Hello Pavel</p> 
<div>{{my_name}}</div> 
<div>{{myWelcome}}</div> 
</body> 
</html> 

而经过负载页面我得到这样的结果

你好帕维尔

帕夏

角种子应用:V0。 1

但我希望看到JSON结果太

+0

使用NG重复,如果你想看到所有的数据 – Jay

+0

尽管整体,至少在对象的形式,至少有一个领域,至少有一些会告诉我,查询ret urned data – user5620472

+0

这个问题已经被问到了吗? –

回答

-1

修改你的HTML这样的。 在这里,您将遍历整个数据数组并打印一个再见。

HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>My view</title> 
</head> 
<body ng-app="myApp.view3" ng-controller="View3Ctrl"> 
<p> Hello Pavel</p> 

<div ng-repeat="row in myWelcome"> 
    <div>{{row.my_name}}</div> 
    <div>{{row.myWelcome}}</div> 
</div> 
</body> 
</html> 
+0

不起作用.............. – user5620472

-1

副本ü要在浏览器中调用,然后,看到了JSON结果的URL服务。 U会看到返回的JSON没有数据对象或状态文本对象。这意味着美国可以使用此代码打印JSON:

angular.module('myApp.view3', ['ngRoute']) 

    .config(['$routeProvider', function($routeProvider) { 
     $routeProvider.when('/view3', { 
      templateUrl: 'view3/view3.html', 
      controller: 'View3Ctrl' 
     }); 
    }]) 

    .controller('View3Ctrl',function($scope, $http) { 
     $scope.my_name = "Pasha"; 
     $http({ 
      method : "GET", 
      url : "http://api.geosvc.com/rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode&format=json" 
     }).then(function mySucces(response) { 
      $scope.myWelcome = response; 
     }, function myError(response) { 
      $scope.myWelcome = response; 
     }); 
    }); 
-1

app.js

var app = angular.module('angularTable', []); 

app.controller('listdata',function($scope, $http) 
{ 
    $scope.users = []; //declare an empty array 
    $http.get("mockJson/mock.json").success(function(response) 
    { 
     $scope.users = response; //ajax request to fetch data into 
    }); 

}); 

的index.html

<tr ng-repeat="user in users"> 
    <td>{{user.id}}</td> 
    <td>{{user.first_name}}</td> 
    <td>{{user.last_name}}</td> 
    <td>{{user.hobby}}</td> 
</tr>