【发布时间】:2020-04-14 13:59:01
【问题描述】:
调用后端(GET /app/getNames)正确返回json数组。
问题是 app.controller.js 中的 $scope.names 始终是一个空数组。
app.controller.js:
myApp.controller('myAppController', ['$scope','$http','myAppService',
function($scope, $http, myAppService) {
$scope.names = [];
myAppService.getNames(function(response) {
$scope.names = response;
});
}
]);
app.service.js
myApp.service('myAppService', ['$http', function($http) {
this.getNames= function() {
return $http({
method: 'GET',
url: '/app/getNames',
});
}
}]);
【问题讨论】:
-
console.log(response)有什么好处 -
除了 Anurag 的问题,我还需要确保这个链接是正确的
url: '/app/getNames',假设 API 和前面部分在同一个主机中? -
使用
console.log(response) I was getting nothing. Url was also correct. I managed to resolve this by changing method tomyAppService.getNames().then(function(response) {...` 然后我更改了$scope.names = response.data以仅获取必要的东西。
标签: javascript angularjs http debugging