【发布时间】:2015-10-21 05:23:48
【问题描述】:
我正在为 http 通信创建一个服务,我需要一些方法来返回一个值.. 但是当我调用成功时出现类型错误;(..
“TypeError:无法读取未定义的属性‘success’”
angular.module('myApp').controller('mainCtrl', function($scope, apiService){
$scope.GetJson = function() {
apiService('post','http://myApiAddress', "{'dataFn': 'userInfo'}")
.success(function(response) { // ERROR !!! TypeError: Cannot read property 'success' of undefined
$scope.userinfo = response;
})
.error(function(error){
console.log(error);
})
}
})
---------------------------------------------------------------------------
var module = angular.module('httpService',[]);
module.factory('apiService',['$http',function($http){
var userAuthority = true; // or false (boolean Value)
var apiService = function (method, url, param){
if(userAuthority){
apiService.idCheck().success( function(res) {
return $http[method](url, param);
})
.error( function(err) {
console.log(err);
})
}
else {
return $http[method](url, param);
}
};
apiService.idCheck = function () {
return $http.get('http://myApiAddress');
};
}])
我正在寻找两个答案之一:
- 为什么成功回调返回错误?
- 如何解决此错误?
【问题讨论】:
-
你想得到什么回应?如果帖子成功了是吗?
-
添加代码最后一行“return apiService;”
-
Gio Perez // 我想获取 JSON 数据,有时会出现错误
标签: jquery angularjs http callback typeerror