Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to both angular and ASP.NET web API. in angular i get values from a form and try to pass it to the server through post request her is the code for angular.

app.controller('AddTrnController',['$scope', 'CategoriesService','$http', function($scope, CategoriesService, $http){
    $scope.categories =[{}];
    $scope.AddTrn = {};
    function init(){
            CategoriesService.getCategories("1")
            .success(function(data){
                $scope.categories = data;
            })
            .error(function(data, status, headers){alert(status);});
        }
    init();

    $scope.change= function(option){
        $scope.AddTrn.TrnID = option.CatID;
        $scope.AddTrn.TrnAccount = 1;
    };


    $scope.UpdateTrans=function(){
        alert("21312");
        $http({method:"post",
            url:'http://localhost:18678/api/Transaction',
            params:"data:$scope.AddTrn"}
            ).success(function(){alert("succeeded");
        }).error(function(){alert("faild");});
    };

}]); 

And her the the code for web API.

public HttpResponseMessage PostTransaction(String transaction)
        {
            if (ModelState.IsValid)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, transaction);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = transaction}));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

I always get failed response from angler and when i make breakpoint in API it dose not trigger.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The params option that you are using adds items to the query string, for a post you will probably want to use the data option instead, try changing the code to this:

$scope.UpdateTrans=function(){
        alert("21312");
        $http({method:"post",
                url:'http://localhost:18678/api/Transaction',
                data:$scope.AddTrn
              }
            ).success(function(){alert("succeeded");
        }).error(function(){alert("faild");});
    };
share|improve this answer
    
I am trying both of them and i get the same result –  user2918388 Jun 24 at 13:46
    
@user2918388 can you use developer tools in your browser, and go to the network tab, and paste the response you are getting from the request? If you tell me what browser you are using I can tell you how to get there –  MDiesel Jun 24 at 13:48
    
I am using chrome –  user2918388 Jun 24 at 13:51
    
Hit 'f12' to open the developer toolbar and then click the 'Network' tab (should be between the 'Elements' and 'Sources' tabs, ) and then issue the request to the api. Once you click on the request to the api there should be a 'Response' tab that will have the info that would help –  MDiesel Jun 24 at 14:03
    
{"Message":"An error has occurred.","ExceptionMessage":"Value cannot be null.\r\nParameter name: entity","ExceptionType":"System.ArgumentNullException" –  user2918388 Jun 24 at 14:08
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.