$ http.get(…)。成功不是函数

【字号: 日期:2024-02-04浏览:21作者:雯心
如何解决$ http.get(…)。成功不是函数?

该.success语法是正确的高达角1.4.3。

对于Angular v.1.6以下的版本,必须使用thenmethod。该then()方法带有两个参数:asuccess和error将与响应对象一起调用的回调。

使用该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格式。 *

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

解决方法

我有此代码:

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

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

TypeError:$ http.get(…)。成功不是函数

有任何想法吗?谢谢

相关文章: