简体   繁体   English

Angular $ http发布返回空数据

[英]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. 我正在设法将其发布到服务器上,我想获取更新的数据并将其加载回相同的JSON对象,但是数据响应为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. 还值得一提的是,初始对象是通过$ rootscope从另一个控制器传递的,并注入到本地范围中。

$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. 另一个选择是使用chrome检查器并检查响应,以确保您确实收到了一些东西。

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 ")] [DataContract(Namespace =“ http://somespace.com ”)]

And my class members had not been given the [DataMember] attribute. 而且我的班级成员没有获得[DataMember]属性。 Web API was not returning back the data. Web API没有返回数据。 I added the [DataMember] attribute and it fixed it. 我添加了[DataMember]属性,并对其进行了修复。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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