【问题标题】:AngularJS $http get data object displays status code instead of responseAngularJS $http 获取数据对象显示状态码而不是响应
【发布时间】:2014-01-09 18:51:17
【问题描述】:

我已经考虑了好几天,但仍然无法弄清楚我做错了什么,所以任何想法甚至是在黑暗中拍摄都值得赞赏。我正在尝试使用 AngularJS $http get 方法向用户显示来自休息服务的响应,但是当我将数据对象打印到控制台时,我始终收到数字 200(我很确定它给我的状态码)。我每次都成功了,在发送请求后,Chrome 调试工具会向我显示包含所有正确数据的响应。我似乎无法让它出现在显示的变量中。如果您有任何想法,请告诉我!谢谢!

我的javascript:

$scope.resendDestinations = [];
$scope.resendDestGet = function () {
    var omtTypeCodeString = '';

    for(var i = 0; i < $scope.mySelections.length; i++){
        if(omtTypeCodeString == ''){
            omtTypeCodeString = $scope.mySelections[i].orderHeader.omtOrderTypeCode;
        }
        else{
            omtTypeCodeString = omtTypeCodeString + ',' + $scope.mySelections[i].orderHeader.omtOrderTypeCode;
        }
    }

    $http({
        method: 'GET',
        url: restService.pom + //service url,
        respondType: 'json',
        headers: {
             'Accept': 'application/json',
             'Content-Type': 'application/json',
             'Access-Control-Allow-Credentials': true
        },
        params: {
            orderTypeCode: omtTypeCodeString,
            transactionCode: 3
            }
        }).success(function (status, data, response, header) {
            console.log("Success!");
            //TODO see if this is being used... has to be
            status = parseInt(status);
            $scope.resendDestinations = data.multipleOrders;
            if (status == 200 && $scope.resendDestinations.length == 0) {
                $scope.bigAlert.title = 'Error',
                $scope.bigAlert.header = 'Search Error';
                $scope.bigAlert.content = 'Current search parameters do not match any results.';
                $scope.showBigAlert();
            }
            else{
                $scope.resendDestinations = data;
                console.log("Data DestinationList here: ");
                console.log($scope.resendDestinations);
                console.log(data.multipleOrders);
                console.log(data);
            }
            $scope.isSearching = false;
        }).error(function (response, data, status, header) {
           //Do error things
        });
    return $scope.resendDestinations;
};

以及服务响应:

[{"destCode":3,"destDescr":"Repository","attributes":null},{"destCode":4,"destDescr":"Pipeline","attributes":null},{"destCode":1,"destDescr":"Processor","attributes":null},{"destCode":2,"destDescr":"DEW","attributes":null}, {"destCode":7,"destDescr":"Management System","attributes":null}, {"destCode":8,"destDescr":"Source","attributes":null}]

【问题讨论】:

  • 更改函数头上状态和数据的顺序。

标签: javascript angularjs rest get


【解决方案1】:

您的参数顺序错误。应该是:success(function(data, status, headers, config)

docs here (click).

此外,.then() 方法通常是首选。如果你切换到那个,你会像这样访问数据:

.then(function(response) {
  var data = response.data;
  var status = response.status;
  //etc
});

【讨论】:

  • 哇...我不敢相信我是那么愚蠢。我有一个偷偷摸摸的怀疑我错过了一些微不足道的东西。谢谢你的回答!我是新手,这对我很有帮助。
  • @AndyVan 没问题!有时,最好休息一下,然后再次查看文档:)
  • 关于使用 .then().success().error() 的好建议
猜你喜欢
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
相关资源
最近更新 更多