$ http.get(...)。成功不是一个功能 [英] $http.get(...).success is not a function

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

问题描述

我有这段代码:

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()方法有两个参数: success 错误将使用响应对象调用的回调。

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()方法,附加一个回调函数返回承诺

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){

   });
}

参见参考这里。

快捷方式方法也可用。

$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

2之间的主要区别在于 .then()调用返回承诺(使用从回调返回的值解决,而 .success()是更传统的注册方式回调并且不会返回承诺

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(...)。成功不是一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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