I consume a REST service using $resource. Why is it that the error callback function is always triggered even though I get a Http: 200 (Ok) response from the server? I've tried 2 ways of setting up the callback functions and both have the same issue.

Here is the Angular controller where I consume the service:

appRoot
.controller(
    'BatchTaskController',
    ['$scope', 'batchTaskService', function ($scope, batchTaskService){

    $scope.runImportIntermediariesTask = function () {
        batchTaskService.runImportIntermediariesTask().$promise.then(
            function (value) { alert('success') },
            function (error) { alert('error') }
            );
    };

    $scope.runImportVantageTransactionsTask = function () {
        batchTaskService.runImportVantageTransactionsTask(
            function () { alert('success'); },
            function () { alert('error'); }
            );
    };

    $scope.runProcessVantageTransactionsTask = function () { batchTaskService.runProcessVantageTransactionsTask(); };
}]);

Here is the Angular service:

var angularVectorServices = angular.module('angularVector.services', ['ngResource']);

angularVectorServices.factory('batchTaskService', ['$resource' , function ($resource) {
return $resource(
    "http://localhost:58655/api/BatchTask/:action",
    {
        action: "@action"
    },
    {
        runImportIntermediariesTask: {
            method: "POST",
            params: {
                action: "RunImportIntermediariesTask"
            }
        },

        runImportVantageTransactionsTask: {
            method: "POST",
            params: {
                action: "RunImportVantageTransactionsTask"
            }
        },

        runProcessVantageTransactionsTask: {
            method: "POST",
            params: {
                action: "RunProcessVantageTransactionsTask"
            }
        }
    }
);
}]);

I am using WebApi. Here is the Server ApiController Code:

public HttpResponseMessage RunImportIntermediariesTask()
    {
      //  _importIntermediariesTask.Run();

        var response = Request.CreateResponse(HttpStatusCode.OK);

        return response;
    }

    public HttpResponseMessage RunImportVantageTransactionsTask()
    {
        //_importVantageTransactionsTask.Run();

        var response = Request.CreateResponse(HttpStatusCode.OK);

        return response;
    }
1

There are 1 best solutions below

1
On

Take this as sample, try to make your action as this one below, setting up your code response code on HttpStatusCode.Whatever:

public HttpResponseMessage GetUser(HttpRequestMessage request, int userId, DateTime lastModifiedAtClient)
{
    var user = new DataEntities().Users.First(p => p.Id == userId);
    if (user.LastModified <= lastModifiedAtClient)
    {
         return new HttpResponseMessage(HttpStatusCode.NotModified);
    }
    return request.CreateResponse(HttpStatusCode.OK, user);
}

Hope this helps.