Javascript $http.get(…)。成功不是一个函数

Javascript $http.get(…)。成功不是一个函数,javascript,angularjs,frontend,Javascript,Angularjs,Frontend,在angular js版本1.2.9中,“success”函数起作用,但在1.6中它使用了“then”函数,该函数起作用,所以我如何使用then转换以下代码 artistControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) { $http.get('js/data.json').success(function(d

在angular js版本1.2.9中,“success”函数起作用,但在1.6中它使用了“then”函数,该函数起作用,所以我如何使用then转换以下代码

artistControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) {
  $http.get('js/data.json').success(function(data) {
    $scope.artists = data;
    $scope.whichItem = $routeParams.itemId;
  });
}]);

.对于1.3以上的版本,不推荐使用success。你应该使用。然后

artistControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) {
  $http.get('js/data.json').then(function(data) {
    $scope.artists = data;
    $scope.whichItem = $routeParams.itemId;
  });
}]);

.对于1.3以上的版本,不推荐使用success。你应该使用。然后

artistControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) {
  $http.get('js/data.json').then(function(data) {
    $scope.artists = data;
    $scope.whichItem = $routeParams.itemId;
  });
}]);

$http的结构已更改。使用
。然后
而不是
。成功

$http.get('js/data.json').then(function(data){
   console.log(data);
}).catch(function(error)){
console.log(error)
});

$http的结构已更改。使用
。然后
而不是
。成功

$http.get('js/data.json').then(function(data){
   console.log(data);
}).catch(function(error)){
console.log(error)
});

.success
语法在1.4.3版之前是正确的

对于Angular v.1.6之前的版本,必须使用
然后
方法。
then()
方法接受两个参数:一个
success
和一个
error
回调,该回调将使用响应对象调用

使用
then()
方法,将
callback
函数附加到返回的
promise

大概是这样的:

app.controller('MainCtrl', function ($scope, $http){
   $http({
      method: 'GET',
      url: 'api/url-api'
   }).then(function (success){

   },function (error){

   });
}
参见参考文献

快捷方式
方法也可用

$http.get('api/url-api').then(successCallback, errorCallback);

function successCallback(response){
    //success code
}
function errorCallback(error){
    //error code
}
2之间的主要区别在于
.then()
调用返回一个
承诺
(通过
回调
返回的值进行解析),而
.success()
是注册
回调
的更传统的方法,不返回
承诺

解决方案

artistControllers.controller('DetailsController', ['$scope', 
  '$http','$routeParams', function($scope, $http, $routeParams) {
    $http.get('js/data.json').then(function(data) {
      $scope.artists = data;
      $scope.whichItem = $routeParams.itemId;
    });
}]);

.success
语法在1.4.3版之前是正确的

对于Angular v.1.6之前的版本,必须使用
然后
方法。
then()
方法接受两个参数:一个
success
和一个
error
回调,该回调将使用响应对象调用

使用
then()
方法,将
callback
函数附加到返回的
promise

大概是这样的:

app.controller('MainCtrl', function ($scope, $http){
   $http({
      method: 'GET',
      url: 'api/url-api'
   }).then(function (success){

   },function (error){

   });
}
参见参考文献

快捷方式
方法也可用

$http.get('api/url-api').then(successCallback, errorCallback);

function successCallback(response){
    //success code
}
function errorCallback(error){
    //error code
}
2之间的主要区别在于
.then()
调用返回一个
承诺
(通过
回调
返回的值进行解析),而
.success()
是注册
回调
的更传统的方法,不返回
承诺

解决方案

artistControllers.controller('DetailsController', ['$scope', 
  '$http','$routeParams', function($scope, $http, $routeParams) {
    $http.get('js/data.json').then(function(data) {
      $scope.artists = data;
      $scope.whichItem = $routeParams.itemId;
    });
}]);
试试这个

$http.get("http://localhost/angulartest/index.php/Items/getItems")
    .then(function (response) {

        console.log(response.data);
        $scope.records = response.data;
    });
试试这个

$http.get("http://localhost/angulartest/index.php/Items/getItems")
    .then(function (response) {

        console.log(response.data);
        $scope.records = response.data;
    });
做三件事

  • 将success()替换为then()
  • 用catch()替换error()
  • 考虑response.data而不是响应
  • 做三件事

  • 将success()替换为then()
  • 用catch()替换error()
  • 考虑response.data而不是响应