Reputation: 33
I am not getting data from Web service's response in angularjs using $http.get method.What cause is this?? It is directly goes to error part.
Web service code:
@GET
@Path("/getEmployeeList")
@Produces(MediaType.APPLICATION_JSON)
public Response getEmployeeList() {
ArrayList<Employee> empList = (ArrayList<Employee>) employeeService.getAllEmployees();
return Response.status(200).entity(empList).build();
}
Angular Js:
<script src="angular.js"></script>
</head>
<body >
<div ng-app="hello">
<h1>Greeting</h1>
<div ng-controller="home" >
<div ng-repeat="a in greeting">
<p>The Id is: {{a.empId}}</p>
<p>The content is: {{a.firstName}}</p>
<p>The content is: {{a.lastName}}</p>
</div>
</div>
</div>
</body>
<script >
var myApp = angular.module('hello', []);
myApp.controller('home',['$scope','$http',function($scope,$http) {
$http.get("http://localhost:8080/restful-jersey-spring-demo/restws/employee/getEmployeeList")
.success(function(data) {
$scope.greeting = data;
}).error(function(data, status, headers, config){
alert("Error")
});
}]);
</script>
</body>
Json Data From Web services: URL:http://localhost:8080/restful-jersey-spring-demo/restws/employee/getEmployeeList
[
{
"empId": 1,
"firstName": "Chirag",
"lastName": "L"
},
{
"empId": 2,
"firstName": "Prashant",
"lastName": "K"
}
]
Upvotes: 2
Views: 137
Reputation: 308
Add the header at response....
@GET
@Path("/getEmployeeList")
@Produces(MediaType.APPLICATION_JSON)
public Response getEmployeeList() {
ArrayList<Employee> empList = (ArrayList<Employee>) employeeService.getAllEmployees();
return Response.status(200).entity(empList).***header("Access-Control-Allow-Origin", "*")***.build();
}
Upvotes: 1
Reputation: 33000
$http does not pass the response data but the response to the success callback. Therefore use:
$http.get("http://localhost:8080/restful-jersey-spring-demo/restws/employee/getEmployeeList")
.success(function(response) {
$scope.greeting = response.data;
}).error(function(response){
alert("Error")
});
For the CORS-error see this thread.
Upvotes: 1