出现此错误:
angular.min.js:122 TypeError:$ http.get(...)。success不是函数 在getUserInfo(app.js:7) 在新的(app.js:12) at Object.invoke(angular.min.js:43) 在Q.instance(angular.min.js:93) 在p(angular.min.js:68) at g(angular.min.js:60) 在g(angular.min.js:61) 在g(angular.min.js:61) 在angular.min.js:60 在angular.min.js:21
这是我的代码:
var gitHub = angular.module('gitHub', []);
gitHub.controller('mainController', ['$scope', '$http', function($scope, $http) {
var $scope.user = '';
function getUserInfo($scope, $http){
$http.get('https://api.github.com/users')
.success(function (result) {
$scope.user = result;
console.log(result);
});
};
getUserInfo($scope, $http);
}]);
这是html
<!DOCTYPE html>
<html ng-app="gitHub">
<head>
<title>Github Users Directory</title>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="mainController">
<div>
<h1>GitHub Users</h1>
Who do you want to search for?<input type="text" name="FindHim" ng-model="queryName" />
<button ng-click="getUserInfo()">Search</button>
</div>
<div>
{{ user }}
</div>
</div>
</body>
</html>
答案 0 :(得分:59)
{@ 1}}和.success
方法已弃用且已removed from AngularJS 1.6。改为使用标准.error
方法。
.then
弃用通知
{@ 1}}旧版承诺方法
$http.get('https://api.github.com/users') .then(function (response) { var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; $scope.user = data; console.log(data); });
和$http
已弃用,将在v1.6.0中删除。改为使用标准.success
方法。— AngularJS (v1.5) $http Service API Reference -- Deprecation Notice
另见SO: Why are angular $http success/error methods deprecated?。
答案 1 :(得分:15)
我认为你在使用angular时需要使用.then而不是.success。
来自文档的示例
char a[20]="wasd", b[20]="asd";
if(*(a+1)==*b) // now we are comparing the values towards which the
printf("yes"); // respective pointers are pointing
以下是$ Http如何使用它的示例:
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
最后你的代码看起来像这样
// 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.
});
答案 2 :(得分:4)
这是有效的
https://docs.angularjs.org/api/ng/service/$http
// 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.
});
答案 3 :(得分:2)
根据您当前的实现,您没有从$scope
将参数(即$http
和getUserInfo
)传递给ng-click="getUserInfo()"
,因此您收到错误。
您不需要将这些作为参数传递为$scope
和$http
,因为它已经注入控制器并在$scope
中定义了该函数。
gitHub.controller('mainController', ['$scope', '$http', function($scope, $http) {
$scope.user = '';
//Redefined function, without arguments
$scope.getUserInfo = function (){
$http.get('https://api.github.com/users')
.success(function (result) {
$scope.user = result;
console.log(result);
});
};
$scope.getUserInfo();
}]);
答案 4 :(得分:1)
你不需要注入$ scope,$ http ..
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
FIND_PACKAGE(PythonInterp 3 REQUIRED)
FIND_PACKAGE(PythonLibs "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" REQUIRED)
FIND_PACKAGE(Boost 1.58.0 REQUIRED COMPONENTS "python-py${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}")
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})
ADD_LIBRARY(override_shared SHARED
override_shared.cc
)
TARGET_LINK_LIBRARIES(override_shared ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
答案 5 :(得分:0)
无需将$ http作为函数参数传递,因为您已将$ http作为依赖项注入控制器。我对代码做了一些修改。请检查它是否适合您。
var gitHub = angular.module('gitHub', []);
gitHub.controller('mainController', ['$scope', '$http', function ($scope, $http) {
$scope.user = '';
$scope.getUserInfo = function() {
$http.get('https://api.github.com/users')
.success(function (result) {
$scope.user = result;
console.log(result);
});
};
$scope.getUserInfo();
}]);
答案 6 :(得分:0)
{{1}}
答案 7 :(得分:0)
根据Angular JS $http
documentation,此系统已从1.4.3 +
中排除。因此,我已从post获得帮助,您可以尝试这种方式
app.controller('MainCtrl', function ($scope, $http){
$http({
method: 'GET',
url: 'api/url-api'
}).then(function (success){
},function (error){
});
}
OR
$http.get('api/url-api').then(successCallback, errorCallback);
function successCallback(response){
//success code
}
function errorCallback(error){
//error code
}
我更喜欢第二个对我来说更灵活的人。
答案 8 :(得分:-1)
function successCallback(response) {
return response
}
$http.get('url')
.then(successCallback)