简体   繁体   English

HTTP响应对象中缺少数据字段。 我该如何访问?

[英]Data field is missing from HTTP response object. How can I access it?

I want to access the order data by using 我想通过使用访问订单数据

$scope.hero.order = response.data;

But it gives an error of undefined and when I check the response object there is no data field which is normally presents , Can anybody highlight my mistake? 但是它给出了一个不确定的错误,当我检查响应对象时,没有通常出现的data字段,有人可以突出显示我的错误吗?

显示图像

OrderService.js OrderService.js

angular.module('Orders')
    .service('OrderService', ['$http', '$state', '$resource', '$q', 'SettingService', '$localStorage', "MessageService",
     function($http, $state, $resource, $q, SettingService, $localStorage, MessageService) {
        var orderResource = $resource(SettingService.baseUrl + "api/orders/:id", {id:'@id'}, {'query':{method:'GET', isArray:false}, 'update':{method:'PATCH'}});
        var service = {
getOrder : function(OrderId, successCallback, failureCallback){
                orderResource.query({id:OrderId}, successCallback, failureCallback);
         }
        }
        return service;
    }]);

You get "error of undefined" because the first argument of orderResource.query() 's success callback is the response body (order object in your case), not response object. 您会收到“不确定的错误”,因为orderResource.query()的成功回调的第一个参数是响应正文(您的情况下为order对象),而不是响应对象。 So, your success callback function should look like: 因此,您的成功回调函数应如下所示:

function(order, getResponseHeaders, status, statusText) {
  $scope.hero.order = order;
  ...
}

You can refer to $resource 's document : 您可以参考$resource文档

Success callback is called with (value (Object|Array), responseHeaders (Function), status (number), statusText (string)) arguments 成功回调使用(value(Object | Array),responseHeaders(Function),status(number),statusText(string))参数调用

What you do now (try to get order object from response.data ) is the behavior of $http ( doc ) -- $http 's success callback will receive a response object, whose data field refer to the http response body. 现在要做的(尝试从response.data获取订单对象)是$httpdoc )的行为- $http的成功回调将接收一个response对象,其data字段引用http响应正文。 However, it is different for $resource -- body, headers, status etc. are all passed as parameter in sequence. 但是,对于$resource来说是不同的-正文,标头,状态等均按顺序作为参数传递。

It seems AngularJS should align the behavior, but unfortunately it doesn't. 看来AngularJS应该使行为一致,但不幸的是,事实并非如此。

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

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