I'm trying to read json from my Web Service.
I get an error when I try to read teh file form the Web Service.
This is my controller:
var myApp = angular.module('myApp',[]);
myApp.controller('ShowServers', function($scope, $http) {
$http.get('http://localhost/espaidiscos/WebService/ws.php/servidors').
success(function(data, status, headers, config) {
$scope.servers = data;
console.log("work");
}).
error(function(data, status, headers, config) {
// log error
console.log("don't work");
});
});
The html:
<html ng-app='myApp'>
<header>
<script src="./assets/js/angular.min.js"></script>
</header>
<body>
<div ng-controller="ShowServers">
<ul>
<li ng-repeat="server in servers">
{{ server.nom }}
</li>
</ul>
</div>
<!--<div ng-controller="GreetingController">
{{ greeting }}
</div>-->
</body>
<!--<script src="./assets/js/angular.min.js"></script>-->
<script src="./assets/js/controller.js"></script>
<!--<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.2/angular.min.js"></script>-->
</html>
I can't get any value on the browser. I'm very newbie, any help would be grateful.
Edit:
This is What I get form the Web Service on this URL:
{"servers": [{"idServidors":"1","nom":"file01","ip":"10.0.0.170"},{"idServidors":"2","nom":"dc","ip":"10.0.0.5"}]}
Edit v2:
I tryied with SoapUI and this is what my web service is returning me:
<html>
<head>
<meta content="HTML Tidy for Java (vers. 26 sep 2004), see www.w3.org" name="generator"/>
<title/>
</head>
<body>{"servers": [{"idServidors":"1","nom":"file01","ip":"10.0.0.170"},{"idServidors":"2","nom":"dc","ip":"10.0.0.5"}]}</body>
</html>
Can the problem comes from here? Don't should be a simple json without body tag and other stuff?
EDIT v3:
Now my web service is working fine and returns a truly json response. I don't get any error on the js chrome console.
This is how my html and angular looks now:
<html ng-app='myApp'>
<header>
<script src="./assets/js/angular.min.js"></script>
</header>
<body>
<div ng-controller="ShowServers">
<ul>
<li ng-repeat="server in servers">
{{ server.nom }}
</li>
</ul>
</div>
<!--<div ng-controller="GreetingController">
{{ greeting }}
</div>-->
</body>
<!--<script src="./assets/js/angular.min.js"></script>-->
<script src="./assets/js/controller.js"></script>
<!--<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.2/angular.min.js"></script>-->
</html>
var myApp = angular.module('myApp',[]);
$http.get('http://localhost/espaidiscos/WebService/ws.php/servidors').success(function(data, status, headers, config) {
$scope.servers = data;
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
}).
error(function(data, status, headers, config) {
// log error
console.log("Error");
});
});
SOLVED:
I copyed the example on w3schools site: http://www.w3schools.com/angular/angular_http.asp
<script>
var MyApp = angular.module('myApp', []);
MyApp.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost/espaidiscos/WebService/ws.php/servidors")
.then(function(response) {
$scope.servers= response.data.servers;
});
});
</script>
