我有这个代码:
app.controller('MainCtrl', function ($scope, $http){
$http.get('api/url-api')
.success(function (data, status, headers, config){
}
}
在我的本地环境中,工作正常,但在服务器中,返回此错误:
TypeError: $http.get(…).success 不是函数
有任何想法吗?谢谢
原文由 Alejo Ribes 发布,翻译遵循 CC BY-SA 4.0 许可协议
.success
语法在 Angular v1.4.3 之前都是正确的。对于 Angular v.1.6 之前的版本,您必须使用
then
方法。then()
方法有两个参数:一个success
和一个error
将使用响应对象调用的回调。使用
then()
方法,将callback
函数附加到返回的promise
。是这样的:
请参阅 此处的参考。
Shortcut
方法也可用。您从响应中获得的数据预计采用
JSON
格式。 JSON 是一种很好的 数据 传输方式,并且在 AngularJS 中使用起来很容易The major difference between the 2 is that
.then()
call returns apromise
(resolved with a value returned from acallback
) while.success()
is更传统的注册方式callbacks
并且不返回promise
。