$http.get(...).success 不是函数 [英] $http.get(...).success is not a function

查看:24
本文介绍了$http.get(...).success 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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

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

In my local enviroment, works ok, but in a server, return this error:

TypeError: $http.get(...).success 不是函数

TypeError: $http.get(...).success is not a function

有什么想法吗?谢谢

推荐答案

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

The .success syntax was correct up to Angular v1.4.3.

对于 Angular v.1.6 以下的版本,您必须使用 then 方法.then() 方法有两个参数:一个 success 和一个 error 回调,它将被一个响应对象调用.

For versions up to Angular v.1.6, you have to use then method. The then() method takes two arguments: a success and an error callback which will be called with a response object.

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

Using the then() method, attach a callback function to the returned 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

The data you get from the response is expected to be in JSON format. JSON is a great way of transporting data, and it is easy to use within AngularJS

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

The major difference between the 2 is that .then() call returns a promise (resolved with a value returned from a callback) while .success() is more traditional way of registering callbacks and doesn't return a promise.

这篇关于$http.get(...).success 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆