javascript - AngularJS $http 获取数据对象显示状态代码而不是响应

标签 javascript angularjs rest get

我已经考虑了好几天了,但仍然无法弄清楚我做错了什么,所以任何想法甚至在黑暗中拍摄的照片都会受到赞赏。我正在尝试使用 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":"处理器","attributes":null},{"destCode":2,"destDescr":"DEW","attributes":null}, {"destCode":7,"destDescr":"管理系统","attributes":null}, {"destCode":8,"destDescr":"Source","attributes":null}]

最佳答案

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

参见 docs here (click).

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

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

关于javascript - AngularJS $http 获取数据对象显示状态代码而不是响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028695/

相关文章:

javascript - 同时暂停 JavaScript

javascript - 指令根模板元素中的属性

c# - 将 int 列表传递给 HttpGet 请求

javascript - Internet Explorer 9 对象检测

javascript - 如何在 css 中点击搜索图标时扩展小方 block 输入框?

javascript - 替换jquery中的span html文本

javascript - AngularJS 指令中的可选表达式属性

javascript - Angular $http 错误回调响应始终未定义

java - 休息 API : Using @QueryParam to load data from MongoDB

Java Jersey 休息服务