0

in my restangular project I am getting this error ,I tried few stuffs but didn't work , this is my ResponseInterceptor

    restangularProvider.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
        if (operation == "getList") {

            var responseData = response.data;
            if (responseData.paginginfo == undefined) return responseData;

            return { pagingInfo: responseData.paginginfo, data: responseData.result };
        }
        if (operation != "get") {
            var item = { status: response.status };
            feedBackFactory.showFeedBack(item);
        }

        return response.data;
    });
}]);

I am using web api ,the code is given below

    [Route("")]
    [HttpGet]
    public IHttpActionResult Get(int page, int size)
    {

        var paginginfo = new PagingModel { CurrentPage = page, RecordsPerPages = size };
        var result = _userManagementService.Getusers(paginginfo);

        if (result.Count == 0) return NotFound();

        return Ok(new  { result, paginginfo});
    }

sample data from my api is

{"result":[{"id":"d615bf59-d089-e411-8295-a0481ca19571","firstname":"binson","lastname":"eldhose","username":"binson143","email":"[email protected]","mobile":"9846369298","userkey":"emp-009","notes":"binson","password":null,"image":"noimage.jpg","createdDate":"2014-12-22T14:47:48.407","status":"Active"},{"id":"0a79c171-c189-e411-8295-a0481ca19571","firstname":"jomon","lastname":"rv","username":"jomon@143","email":"[email protected]","mobile":"9876543478","userkey":"emp-007","notes":"good progtam","password":null,"image":"noimage.jpg","createdDate":"2014-12-22T13:01:06.817","status":"Active"},{"id":"f02053ff-b989-e411-8294-a0481ca19571","firstname":"Mohit","lastname":"Sarma","username":"[email protected]","email":"[email protected]","mobile":"9867564534","userkey":"Emp-008","notes":"A good programmer too","password":null,"image":"noimage.jpg","createdDate":"2014-12-22T12:07:48.35","status":"Active"},{"id":"e69a316c-bf89-e411-8295-a0481ca19571","firstname":"nizar","lastname":"kp","username":"nizar143","email":"[email protected]","mobile":"98675645321","userkey":"emp-009","notes":"good programmer","password":null,"image":"noimage.jpg","createdDate":"2014-12-22T12:46:38.023","status":"Active"},{"id":"5c2e3ede-b989-e411-8294-a0481ca19571","firstname":"Rins","lastname":"Dominc","username":"rins143","email":"[email protected]","mobile":"98675654345","userkey":"Emp-001","notes":"Good employee","password":null,"image":"noimage.jpg","createdDate":"2014-12-22T12:06:52.85","status":"Active"}],"paginginfo":{"currentPage":1,"totalRecords":5,"recordsPerPages":10,"startRecord":1,"endRecord":5,"totalPages":1}}

what shoud I do to override this error, please not I cant change my api like

 return Ok(new object[] { result, paginginfo});
3
  • Check out this answer: stackoverflow.com/questions/22012655/… Commented Dec 22, 2014 at 13:42
  • @MichaelLo tried first,but not worked... I am using retangular 1.4 Commented Dec 22, 2014 at 13:46
  • if your getList method returns object instead of array, use customGET method. customGET will allow you to return object instead of array. Commented Dec 24, 2014 at 9:18

1 Answer 1

1
.config(function(RestangularProvider){
   RestangularProvider.setBaseUrl('/api');
   RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
      var extractedData;
      if (operation === "getList") {
        extractedData = data.result;
      } else {
        extractedData = data;
      }
      return extractedData;
    });
})

you should add response interceptor

Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful to get an original json response from an API vs. Restangular manipulating the response to an array.

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.