0

I am relatively new to Angular js and trying to use promises along with services and got reference http://plnkr.co/edit/b4HPbX2olM745EfHVcc6?p=preview. But in my application I am getting response as {"Response":"exception while loading Reports List","error":"Exception getting all records.","status":"failure"}. When I get response like this, I need to show an alert message with the message in "error" (i.e., Exception getting all records) and set $scope.data to [] in my controller.What are the changes I need to make to services and controller to handle this. Any help is much appreciated.

In services :

return $q.when(originalRequest)
          .then(function(res) {
            data = res.ResultSet.Response;
            return data;
          });

In Controller,

  DashboardsDataService.getNetSpendOverTimeData()
      .then(function(data) {
        $scope.data = data;
      });

The following is my original request to Java action class:

var originalRequest = $.ajax({
                            async : false,
                            url : "/dash2/dashd2ajax.do",
                            type : "POST",
                            data : {
                                action : 'getNetSpendOverTime',
                                customerId : selectedAccTd,
                                carriersId : selectedCarriers,
                                fromDate : fromDate,
                                toDate : toDate,
                                modes : selectedModes,
                                services : selectedServices,
                                dateType : selectedDateType,
                                lanesList : selectedLaneList
                            },
                            dataType : "json"
                        });
                 return $q.when(originalRequest)
                  .then(function(res) {
                    data = res.ResultSet.Response;
                    return data;
                  });
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
smart987
  • 834
  • 2
  • 14
  • 34
  • Have you tried reading [the documentation](https://docs.angularjs.org/api/ng/service/$q)? – hon2a Nov 25 '14 at 11:42
  • Yes and I even made the changes to services as, return $q.when(originalRequest) .then(function(res) { data = res.ResultSet.Response; return data; }, function(err){ alert( err.ResultSet.error); data = []; return data; });. But it is not working as I expected. Could you please help me to fix it? – smart987 Nov 25 '14 at 12:03
  • Please show where the `originalRequest` comes from. – hon2a Nov 25 '14 at 12:12
  • I have updated the question with the originalRequest – smart987 Nov 25 '14 at 12:18

1 Answers1

1

If what you're asking is "how do I turn request success into a failure based on result data", then take a look at the following example:

return $q.when(originalRequest).then(function (res) {
    if (res.ResultSet.error) {
        return $q.reject(res.ResultSet.error);
    } else {
        return res.ResultSet.Response;
    }
});

Using $q.reject() turned your data into a real "promise failure", so in your controller, you can use the normal promise API:

doSomethingAsynchronous().then(function (data) {
    $scope.data = data;
}, function (error) {
    $scope.data = [];
    alert(error);
});
hon2a
  • 7,006
  • 5
  • 41
  • 55
  • Thanks this is what I am looking for. Do you have any pointers for me to a similar issue at http://stackoverflow.com/questions/26057909/angularjs-issue-in-relacing-ajax-request-with-promises-in-service?rq=1 to use http service. – smart987 Nov 26 '14 at 06:23