Can not get response back from Java REST Service using ngResource and $resource

33 views
Skip to first unread message

Jayanta Pramanik

unread,
Jul 9, 2014, 4:47:04 AM7/9/14
to ang...@googlegroups.com
I'm trying fetch data by calling Java REST service from AngularJS web portal. I'm using ngResource. 

Call From Angular Service :

    app.factory('UserFactory', function ($resource) {
        return $resource('http://localhost:8080/demoApp/service/users', {}, {
            show: { method: 'GET', isArray: true },
            update: { method: 'PUT', params: {id: '@id'} },
            delete: { method: 'DELETE', params: {id: '@id'} }
        })
    });

Angular Controller :

function userListController($scope, UserFactory){    
    $scope.showUser = function () {
        $scope.fetchedData = [];        
        $scope.users = UserFactory.show();
        $scope.fetchedData.push($scope.users);        
    };

It is calling Java Service perfectly (As my debugger shows) but NO OUTPUT returned at Angular side. 
Even I can get the result if I put the URL directly at the browser :
   
    RESULT :   [{"id":1,"firstName":"Jayanta","lastName":"Pramanik"}]

But in Angular-JS controller I can not get any result !!

For your understanding -
Here is my Java REST Service method definition -
     
       @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getAllUsersInJSON() {
 /* ------ Some Hard coded values are set into an ArrayList ------*/
            List<User> userList = new ArrayList<User>();
   User usr1 = new User();
   usr1.setId(1);
   usr1.setFirstName("Jayanta");
   usr1.setLastName("Pramanik");
            userList.add(usr1);
            return userList;
    } 



Any idea please !

Thanks in advance -

Jayanta P.
Kolkata,  India. 


Sander Elias

unread,
Jul 9, 2014, 8:55:08 AM7/9/14
to ang...@googlegroups.com

Hi Jayanta,

Sounds like a CORS issue to me. Have a look at the developer’s console, and see what the error is in there!
probably there is something like this: Origin http://localhost:8080/demoApp/service/users is not allowed by Access-Control-Allow-Origin in there.

If that is the case, you need to enable CORS on your server. not an AngularJS issue.

Regards
Sander

Jayanta Pramanik

unread,
Jul 9, 2014, 9:25:09 AM7/9/14
to ang...@googlegroups.com
Thanks for the answer. Yes I did that and now it is working.

Regards.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/NO0ntSivtUI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages