加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:39:44 所属栏目:安全 来源:网络整理
导读:1.在使用Angular 1.6版本的$http服务时会抛出异常 :$http.get(...).success is not a function 或者$http(...).success is not a function 异常代码如下: //请求api$http.get('/api/user/showname',{ params: { name: '张三' }}).success(function (data,s

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

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

异常代码如下:

//请求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);
});

异常信息如下:

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规则。

更改写法:

$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来监视属性或对象的变化

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读