$http.get(..).成功不是一种功能



我有这个代码:

app.controller('MainCtrl', function ($scope, $http){
$http.get('api/url-api')
.success(function (data, status, headers, config){
}
}

在我的本地环境中,工作正常,但在服务器中,返回此错误:

类型错误: $http.get(...).成功不是一种功能

有什么想法吗?谢谢

.success语法在 Angular v1.4.3 之前是正确的。

对于 Angular v.1.6 之前的版本,您必须使用then方法。then()方法采用两个参数:successerror回调,将使用响应对象调用。

使用then()方法,将callback函数附加到返回的promise

像这样:

app.controller('MainCtrl', function ($scope, $http){
$http({
method: 'GET',
url: 'api/url-api'
}).then(function (response){
},function (error){
});
}

请参阅此处的参考资料。

Shortcut方法也可用。

$http.get('api/url-api').then(successCallback, errorCallback);
function successCallback(response){
//success code
}
function errorCallback(error){
//error code
}

从响应中获取的数据应采用JSON格式。JSON是传输数据的好方法,在AngularJS中很容易使用

。2 之间的主要区别在于.then()调用返回一个promise(使用从callback返回的值解析),而.success()是更传统的注册callbacks的方式,并且不返回promise

这可能是多余的,但上面投票最多的答案说.then(function (success),这在 Angular 版本1.5.8中对我不起作用。而是使用response然后在块内response.data我得到了我正在寻找的 json 数据。

$http({
method: 'get', 
url: 'data/data.json'
}).then(function (response) {
console.log(response, 'res');
data = response.data;
},function (error){
console.log(error, 'can not get data.');
});

如果您尝试从 2017 年 10 月 21 日起使用 AngularJs 1.6.6,则以下参数将作为 .success 工作并且已耗尽。.then() 方法有两个参数:一个响应和一个错误回调,将使用响应对象调用。

$scope.login = function () {
$scope.btntext = "Please wait...!";
$http({
method: "POST",
url: '/Home/userlogin', // link UserLogin with HomeController 
data: $scope.user
}).then(function (response) {
console.log("Result value is : " + parseInt(response));
data = response.data;
$scope.btntext = 'Login';
if (data == 1) {
window.location.href = '/Home/dashboard';
}
else {
alert(data);
}
}, function (error) {
alert("Failed Login");
});

上面的截图适用于登录页面。

相关内容

最新更新