Angular 1.6提示$http.get(...).success is not a function

1.在使用Angular 1.6版本的$http服务时会抛出异常:$http.get(...).success is not a function

或者$http(...).success is not a function

异常代码如下:

[javascript] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. //请求api  
  2. $http.get('/api/user/showname', {  
  3.     params: {  
  4.         name: '张三'  
  5.     }  
  6. }).success(function (data, status, config, headers) {  
  7.     console.info(data);  
  8.     alert(data);  
  9. }).error(function (data) {  
  10.     console.info(data);  
  11. });  
//请求api
$http.get('/api/user/showname', {
    params: {
        name: '张三'
    }
}).success(function (data, status, config, headers) {
    console.info(data);
    alert(data);
}).error(function (data) {
    console.info(data);
});

异常信息如下:

[javascript] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. angular.js:14328TypeError: $http.get(...).success is not a function  
  2.     at new <anonymous> (test2.html:20)  
  3.     at Object.invoke (angular.js:4842)  
  4.     at R.instance (angular.js:10695)  
  5.     at n (angular.js:9572)  
  6.     at g (angular.js:8881)  
  7.     at angular.js:8746  
  8.     at angular.js:1843  
  9.     at m.$eval (angular.js:17972)  
  10.     at m.$apply (angular.js:18072)  
  11.     at angular.js:1841  
angular.js:14328TypeError: $http.get(...).success is not a function
    at new <anonymous> (test2.html:20)
    at Object.invoke (angular.js:4842)
    at R.instance (angular.js:10695)
    at n (angular.js:9572)
    at g (angular.js:8881)
    at angular.js:8746
    at angular.js:1843
    at m.$eval (angular.js:17972)
    at m.$apply (angular.js:18072)
    at angular.js:1841

究其原因,新版本的AngularJs中取消了success和error,用promise规则。

更改写法:

[javascript] view plain copy
print ? 在CODE上查看代码片 派生到我的代码片
  1. $http.get('/api/user/showname2', {  
  2.     params: {  
  3.         name: '张三',  
  4.         age: 'abc'  
  5.     }  
  6. }).then(function (result) {  //正确请求成功时处理  
  7.     console.info(result);  
  8.     alert(result.data);  
  9. }).catch(function (result) { //捕捉错误处理  
  10.     console.info(result);  
  11.     alert(result.data.Message);  
  12. });  
$http.get('/api/user/showname2', {
    params: {
        name: '张三',
        age: 'abc'
    }
}).then(function (result) {  //正确请求成功时处理
    console.info(result);
    alert(result.data);
}).catch(function (result) { //捕捉错误处理
    console.info(result);
    alert(result.data.Message);
});
正常相应:

异常400相应:



更多:

AngularJS $http简介1

使用$watch来监视属性或对象的变化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值