AngularJS $http返回的数据类型

AngularJS 1.6.4 调用$http服务,是如下样式:

$http.get('/someUrl', config).then(successCallback, errorCallback);

 

项目中代码:

angular.module("myApp").controller("mainCtrl",["$http","$scope",function($http,$scope){
	$http.get("data/positionList.json").then(function(resp){
		$scope.lists=resp;
		console.log($scope.lists);
	},function(){

	});
}]);

一直在纠结怎么lists就是get不到正确的数据,在页面中展示的内容很奇怪。 后来在控制台打印出来结果信息才发现:

请求结果resp的是包含请求数据,状态码,回应头信息,请求字符码的对象。在控制台结果如下:

Object { data: Array[4], status: 200, headers: wd/<(), config: Object, statusText: "OK" }

展开如下:

 

所以要获取数据,应该获取对象中的data属性,使用resp.data:

angular.module("myApp").controller("mainCtrl",["$http","$scope",function($http,$scope){
    $http.get("data/positionList.json").then(function(resp){
        $scope.lists=resp.data;
        console.log($scope.lists);
    },function(){

    });
}]);

这次获取的对象才是数据:

Array [ Object, Object, Object, Object ]

转载于:https://www.cnblogs.com/happyfish321/p/6831037.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值