无法从服务获取控制器中的数据

时间:2023-01-30 09:43:27

app.service('customersService', function ($http) {
    this.getCustomer = function (id) {
        $http({
            method: 'GET',
            url: '/getCustomer',
            params: {id: id},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        }).success(function(data) {
            console.log(data);
            return data;
        })
    };
});

Cannot get the data in the controller from the service The problem with this code is when i try to run this

无法从服务获取控制器中的数据,此代码的问题是当我尝试运行此代码时

customersService.getCustomer(customerID).then

It generates an error mentioned below:

它产生如下所述的错误:

angular.js:13294 TypeError: Cannot read property 'then' of undefined.

角。无法读取未定义的属性“then”。

The main thing is the call to the service is generated and if i try to print the results on the console in the service the data is there. However i cannot get the data in my controller.

最重要的是生成对服务的调用,如果我尝试在服务的控制台上打印结果,数据就在那里。但是我无法在控制器中获取数据。

3 个解决方案

#1


2  

You get that error becuase you are not returning the promise from $http GET request.

您得到这个错误是因为您没有返回$http get请求的承诺。

Edit your code like this:

编辑您的代码如下:

app.service('customersService', function ($http) {
    this.getCustomer = function (id) {
        return $http({
            method: 'GET',
            url: '/getCustomer',
            params: {id: id},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        });
    };
});

And then with .then() in your controller, you handle the response data.

然后在控制器中使用then()处理响应数据。

app.controller('myController', function($scope, customerService){

    customersService.getCustomer(customerID)
      .then(function(response){
        $scope.data = response;
      })
})

#2


0  

You simply forget to return the $http promise that is why undefined is returned. You need to do the following:

您只是忘记返回$http承诺,这就是未定义返回的原因。你需要做以下工作:

...
return $http({ ...

#3


0  

Try given code.

尝试给出代码。

app.service('customersService', function ($http) {
  var getCustomer = function (id) {
      return $http({
            method: 'GET',
            url: '/getCustomer',
            params: {id: id},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        })
  };

    var customersService = {
            getCustomer: getCustomer
    }
    return ProfileService;

});

#1


2  

You get that error becuase you are not returning the promise from $http GET request.

您得到这个错误是因为您没有返回$http get请求的承诺。

Edit your code like this:

编辑您的代码如下:

app.service('customersService', function ($http) {
    this.getCustomer = function (id) {
        return $http({
            method: 'GET',
            url: '/getCustomer',
            params: {id: id},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        });
    };
});

And then with .then() in your controller, you handle the response data.

然后在控制器中使用then()处理响应数据。

app.controller('myController', function($scope, customerService){

    customersService.getCustomer(customerID)
      .then(function(response){
        $scope.data = response;
      })
})

#2


0  

You simply forget to return the $http promise that is why undefined is returned. You need to do the following:

您只是忘记返回$http承诺,这就是未定义返回的原因。你需要做以下工作:

...
return $http({ ...

#3


0  

Try given code.

尝试给出代码。

app.service('customersService', function ($http) {
  var getCustomer = function (id) {
      return $http({
            method: 'GET',
            url: '/getCustomer',
            params: {id: id},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        })
  };

    var customersService = {
            getCustomer: getCustomer
    }
    return ProfileService;

});