2

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"
}
]
3
  • console.info error details or put your browser in developer mode to get error details. I suspect the use of absolute uri. did you try using this one /restful-jersey-spring-demo/restws/employee/getEmployeeList? Commented Aug 11, 2015 at 13:33
  • I am getting following Error:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at localhost:8080/restful-jersey-spring-demo/restws/employee/…. (Reason: CORS header 'Access-Control-Allow-Origin' missing). Commented Aug 11, 2015 at 13:40
  • According to CORS, a different port is a different origin. You'll need to make sure your Jersey response includes a header of Access-Control-Allow-Origin: * . Commented Aug 11, 2015 at 15:52

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

then debug this function: set a breakpoint in your success callback and examine the value which is passed.
I am getting following Error:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at localhost:8080/restful-jersey-spring-demo/restws/employee/…. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
I would recomend using .then(function successHandler() {}, function errorHandler() {}) because it returns promise implicitly which is not the case with .success() and .error(). Because of this, in provided example it is imposible to chain additional promise handlers which is very often needed during normal development.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.