无法使用$http读取未定义的属性“成功”

Cannot read property 'success' of undefined using $http

本文关键字:属性 成功 未定义 读取 http      更新时间:2023-09-26

在我的控制器中,我这样做

topicContent.request().success(function(data){
        $scope.threadContent = data;
      });

在我的工厂里,我写道:

app.factory('topicContent', ['$http', function($http){
    return
            var query = function() {
                  $http({
                    url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
                    method: "GET"
                });
            }
            return {
                request : function(){
                    return query();
                }

            }
        }]);

我已经检查了我的服务没有依赖项错误。知道为什么它说成功是不确定的吗?

您不会从 query() 返回任何内容。尝试:

app.factory('topicContent', ['$http', function($http){
        var query = function() {
              // HERE!!
              return $http({
                url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
                method: "GET"
            });
        }
        return {
            request : function(){
                return query();
            }

        }
    }]);

这应该可以解决它。

var query = function() {
      return $http({
        url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
        method: "GET"
    });
}

删除第 2 行的返回语句:

app.factory('topicContent', ['$http', function($http){
    return

也许以下内容会有所帮助:

在控制器中:

topicContent.request().then(function(data){
    $scope.threadContent = data;
  });

并在服务

app.factory('topicContent', ['$http', function($http){
var query = function() {
    **return** $http({
        url: "http://www.corsproxy.com/daysof.me/lowyat/thread.php",
        method: "GET"
    }).then(function(response){
        **return** response.data;
    });
}
return {
    request : query
    }
}]);

请注意服务的查询函数中的 2 个 return 语句,以及服务返回的简单对象。