3

Below is my code

MY_MODULE.factory("SOME_API", ['$resource', '$http', '$rootScope', function($resource, $http, $rootScope){

  return $resource("/appi/get/", {responseFormat: 'json', responselanguage:'English', pageno:1}, {
    search:{
      method:'GET',
      transformResponse: [function (data, headersGetter) {
          console.log(data);
          // you can examine the raw response in here
          $log.info(data);
          $log.info(headersGetter());
          return {tada:"Check your console"};
      }].concat($http.defaults.transformResponse),
      params:{
        param1:'SOME_DATA',
        param2:'SOME_DATA2',
      }
    }
  }
}

I m using angular 1.0.7, can't figure out why my transformResponse is not called. I believe this version of angular supports transformResponse, if not how to implement similar callback.

Sudesh
  • 1,129
  • 3
  • 14
  • 29
  • I have posted an answer at [this](http://stackoverflow.com/a/26354096/3214001). It might be helpful. – soytian Oct 14 '14 at 06:38

3 Answers3

1

transformResponse is not a parameter to $resource. The response interceptor are configured on $httpProvider. See the documentation of $http (Section Response Interceptor).

Keep in mind that once configured these interceptors would run for each request that was made by $http.

Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • I referred a github code, https://github.com/nameoffnv/funsite/blob/c6b7576e292a0feb0e2bf317de9d7b517c73905f/static/js/services.js, so i believe i can use it that way. Also transformResponse is not a parameter to resource it is parameter for a resource action which inturn is sent to $http internally. – Sudesh Aug 09 '13 at 05:57
  • Ok, i see that in the $http documentation there is indeed parameter available. Can you try to invoke the service with using `$http` instead of resource. – Chandermani Aug 09 '13 at 08:42
0

Is it possible to use AngularJS 1.2?

I had the exact same problem with 1.0.8. After I changed to angular-1.2.0-rc.2, my resource's transformResponse callback started being called.

rvl
  • 81
  • 5
  • I knew i can upgrade, but as i had legacy code could not do that. But I found work around, had to create a custom resource function which took similar input as default resource function but worked as I wanted. I will post the solution as answer. – Sudesh Oct 07 '13 at 04:45
0

Below is the solution, basically a wrapper to $http, which emulates default resource

$rootScope.woiresource = function (url, defaults, actions) {
  var $res = {};

  for (var i in actions) {

    var default_params = {};

    for (var di in defaults) {
      default_params[di] = defaults[di];
    }

    for (var ai in actions[i].params) {
      default_params[ai] = actions[i].params[ai];
    }

    $res[i] = (function (url, method, default_params) {
      return function (params, callback, headers) {

        if (typeof params == 'function') {
          callback = params;
          params = {};
        }

        if (typeof headers == 'undefined') {
          headers = {};
        }

        for (var pi in params) {
          default_params[pi] = params[pi];
        }

        return $http({
          url: url,
          method: method,
          transformResponse: [function (strData, headersGetter) {
              //Do Something

          }].concat($http.defaults.transformResponse),
          params: default_params,
          headers: headers,

        }).success(function (data) {
          callback(data);
        });
      };
    })(url, actions[i].method, default_params);
  }

  return $res;
};

Parameters are similar to default resource just changed the working, hope this helps some people.

Sudesh
  • 1,129
  • 3
  • 14
  • 29