简体   繁体   中英

Angular $http post returning empty data

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.

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.

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.

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;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM