Javascript 无法从中获得承诺
我使用AngularHTTP get请求从.NETWebAPI web服务获取数据,Angular网站和web服务都部署在同一个web服务器上。以下是从服务获取所有网站json列表的服务代码:Javascript 无法从中获得承诺,javascript,.net,angularjs,asp.net-web-api2,Javascript,.net,Angularjs,Asp.net Web Api2,我使用AngularHTTP get请求从.NETWebAPI web服务获取数据,Angular网站和web服务都部署在同一个web服务器上。以下是从服务获取所有网站json列表的服务代码: this.getAllWebsites = function (callback) { return $http.get($rootScope.url + 'GetAllWebsites').success(function (data) { callback(da
this.getAllWebsites = function (callback) {
return $http.get($rootScope.url + 'GetAllWebsites').success(function (data) {
callback(data)
});
}
这就是我在控制器中所做的:
//function triggered at the intialization of controller
$scope.init = function () {
$scope.model = [];
websiteService.getAllWebsites(function (data) {
$scope.model = data;
websiteService.paginate($scope);
});
};
我的url是正确的,问题是当我调试JavaScript代码时,服务返回数据,否则它不会返回任何数据。我知道问题在承诺中,不会被退回,请建议任何解决方法
this.getAllWebsites = function (callback) {
return $http.get($rootScope.url + 'GetAllWebsites').then(function (response) {
return response.data
});
}
内部控制器
//function triggered at the intialization of controller
$scope.init = function () {
$scope.model = [];
websiteService.getAllWebsites().then(function (data) {
$scope.model = data;
websiteService.paginate($scope);
});
};
请尝试“返回回拨数据;”在服务函数中,GET调用应该是这样的:$http.GET'/someUrl',config.thensuccescallback,errorCallback@Dario如果HTTP请求返回promise,那么我们为什么要使用它呢?