【问题标题】:Angularjs Get MethodAngularjs 获取方法
【发布时间】:2017-03-02 23:11:41
【问题描述】:

在控制台中出现以下错误,并且数据未从 mysql 中获取。任何人都可以就以下错误提出建议:

使用过angularjs、php、mysql和materializecss

错误:$http.get(...).success 不是函数

这是我的控制器的代码:

.controller('dashBoardCtrl', ['$scope', '$http', function ($scope, $http) 
{ 
    $http.get("dbconnection/db_read.php") 
        .success(function(data)
        { 
            $scope.data = data; 
            console.log(data); 
        }) 
        .error(function() 
        { 
            $scope.data = "error in fetching data"; 
        }); 
    }]
);

【问题讨论】:

  • .controller('dashBoardCtrl', ['$scope', '$http', function ($scope, $http) { $http.get("dbconnection/db_read.php") .success (function(data){ $scope.data = data; console.log(data); }) .error(function() { $scope.data = "获取数据时出错"; }); }]) ;跨度>
  • 如果您需要添加到您的问题中,只需使用编辑按钮而不是评论。然后就说你添加了一个编辑,也许把 EDIT 加粗。并围绕您要添加的内容添加一些上下文。

标签: php angularjs


【解决方案1】:

你可以试试

$http.get().then(function(result){console.log(result)});

我相信.success 已被弃用。

【讨论】:

  • 在 1.5 中已弃用。它在 1.6 中被完全删除
【解决方案2】:

你很有可能使用 AngularJS 1.6+

.success(fnSuccess).error(fnError) 不再存在。

请改用.then(fnSuccess, fnError)

所以

$http.get(...).success(function(){})

现在

$http.get(...).then(function success() {}, function error() {});

【讨论】:

  • 感谢大家的建议..问题已解决
【解决方案3】:

我们来看看the documentation

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

因此,您将成功和错误回调传递给 then() 而不是 success()

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 2013-12-10
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多