Consume Async REST web service in AngularJS

572 views
Skip to first unread message

Mahendra Gurav

unread,
Sep 20, 2015, 10:43:26 PM9/20/15
to AngularJS
Hi,

  I have Java REST web service, which is implemented as async. Need to call that service in my angularjs application. I am able to call normal REST service using get method, but what should I use to call async rest service. Please give me sample code or any references for that.


Thanks,
Mahendra

Sander Elias

unread,
Sep 21, 2015, 3:43:21 AM9/21/15
to AngularJS
Hi Mahendra,

There is no difference in the client side between you solving something sync or async on the server. It's always async on the client anyway. There is probebly something you need to change in your server's coding

Regards
Sander

András Csányi

unread,
Sep 21, 2015, 3:57:58 AM9/21/15
to ang...@googlegroups.com
Hi Mahendra,

As Sander described there is no difference between sync or async on the client side if you use Angular. Both managed by promises (Q library of Angularjs). It might be an issue how to deal with promises. Please check the document about Q library where you can find examples.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, 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.



--
--  Csanyi Andras (Sayusi Ando)  -- http://sayusi.hu -- http://facebook.com/andras.csanyi
--  ""Trust in God and keep your gunpowder dry!" - Cromwell

Mahendra Gurav

unread,
Sep 24, 2015, 2:09:12 AM9/24/15
to AngularJS, sayus...@sayusi.hu
Hi,

   I am using REST web service which is developed in Java. Below is the description how that service is implemented

    1. AsynContext, with Start and Complete method

   Which returns the data asynchronously. We checked in cURL  and we are able to read the data as it comes.

But in when we consume that web service in AngularJS we are not able to get data. Here we used defer and promise like below

service.GetAllSensors=function(){

 var deferred = $q.defer();
  var promise = $http({method:'GET', url:'http://localhost:8080/xxxx/xx/xx'})
  .success(function (data) {
   alert("return success "+ deferred.resolve(data));
    return deferred.resolve(data);
  })
  .error(function (data) {
   alert("error" + data);
  return {"status": false};
  });
  return deferred.promise;
};


//Calling method 
SensorDataService.GetTest().then(function(promise) {
alert("promise result"+ promise);
});


So could anybody tell me why I am not getting the data from the service here.

Thanks,
Mahendra

Sander Elias

unread,
Sep 24, 2015, 5:24:17 AM9/24/15
to AngularJS, sayus...@sayusi.hu
Hi Mahendra,

Can you show me an example that works with a plain xmlHttpRequest()? When you do that, I can tell you how to fix your angular code. It looks like it that your server is using a non-default/standard way off returning it's result.

Regards
Sander

Daniel Lidström

unread,
Sep 25, 2015, 8:25:36 AM9/25/15
to AngularJS, sayus...@sayusi.hu
Try this:

service.GetAllSensors = function () {
  return $http.get('http://localhost:8080/xxxx/xx/xx')
    .then(function x) { return x.data; });
};
Reply all
Reply to author
Forward
0 new messages