Angular $http post returning empty data

1.7k Views Asked by At

I'm managing to post to the server OK, I'd like to get the updated data and load it back into the same JSON object, but the data response is null.

$scope.saveDetails = function() {
    $http({
        method : 'POST',
        url : '/service/rest/orders',
        data : $scope.orderDetails
    })
    .success(function(data, status) {
        $scope.orderDetails = data;                 
    })
    .error(function() {
        alert('error');
    });                  
}

Also worth mentioning, that the initial object is being passed from another controller via $rootscope and injected into the local scope.

$scope.orderDetails = $rootScope.newOrder;

Thanks for any help.

3

There are 3 best solutions below

2
Ryan Knell On BEST ANSWER

Your code looks fine, I would be checking the backend to make sure data is actually being sent. Another option would be to use the chrome inspector and check the response to make sure you are actually getting something back.

0
Desmond On

It turns out it was returning the whole object and the order was deeper down, I didn't see that in my console at first.

$scope.orderDetails = data.order;  

Thanks for all replies.

0
Norbert Norbertson On

In case anyone else runs into this, in my case I had a class with a data contract attribute applied:

[DataContract(Namespace = "http://somespace.com")]

And my class members had not been given the [DataMember] attribute. Web API was not returning back the data. I added the [DataMember] attribute and it fixed it.

[DataMember] public int NumberUpdated { get; set; }
[DataMember] public int NumberInserted { get; set; }
[DataMember] public List<ServicesListImport> InvalidRows {get; set;}