Angular $ http.get方法出错

时间:2017-03-26 05:59:24

标签: javascript angularjs rest api

我想使用REST api响应来检索数据并在我的html页面中显示。这是我到目前为止编写的代码。任何人都可以帮助我编写正确的代码,因为我是新手使用Web服务。

当我使用rest API插入数据库时​​,它会被插入。我希望通过其余的API响应插入到数据库中的数据,我想在网页上显示相同的结果。

<!DOCTYPE html>
<html>
<head>
  <title>AngularJS File Upoad Example with $http and FormData</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
</head>

<body ng-app="myApp">
<div ng-controller="MyController">
<table>
	<tr>
		<td><input type="name" ng-model="contact.name"></td>
		<td><input type="email" ng-model="contact.username"></td>
		<td><input type="password" ng-model="contact.password"></td>
		<td><button ng-click="addContact()">Sign In</button></td>
		<td><button ng-click="viewContact()">view</button></td>
	</tr>
</table>
</div>
<script type="text/javascript">
	var myApp = angular.module('myApp',[]);
	myApp.controller('MyController',['$scope','$http',function($scope,$http){
    $scope.addContact = function(){
        console.log($scope.contact);
        $http.post('http://localhost:8085/useraccount/register/doregister',$scope.contact);
    };
    $scope.viewContact = function(){
    	console.log($scope.contact);
    	$http.get('http://localhost:8085/useraccount/register/doselect',$scope.contact);
    };
	}]);
	
</script>
</body>
</html>

<!-- begin snippet: js hide: false console: true babel: false -->

1 个答案:

答案 0 :(得分:1)

您错误地使用了$http.get

$scope.viewContact = function(){
    console.log($scope.contact);
    $http.get('http://localhost:8085/useraccount/register/doselect')
      .then(function(response) { 
        $scope.contact = response;
      });
};

$http.get会返回一个承诺,您必须使用.then来访问API响应。