请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Possibly unhandled rejection: error

老师,请帮我指正一下这个报错,谢谢!

这是http.js

'use strict';
angular.module('app').config(['$provide', function($provide){
  $provide.decorator('$http', ['$delegate', '$q', function($delegate, $q){
    $delegate.post = function(url, data, config) {
      var def = $q.defer();
      $delegate.get(url).then(function(res){
        def.resolve(res);
      },function(err){
        def.reject(err);
      });
      return {
        success: function(cb){
          def.promise.then(cb);
        },
        error: function(cb) {
          def.promise.then(null, cb);
        }
      }
    }
    return $delegate;
  }]);
}]);

这是registerCtrl.js

'use strict';
angular.module('app').controller('registerCtrl',['$interval','$http','$scope','$state',function($interval,$http,$scope,$state){
	$scope.submit = function(){
		$http.post('data/regist.json',$scope.user).then(function successCallback(res){
			console.log(res.data);
		},function errCallback(err){
			console.log(err);
		});
	}
	var count = 60;
	$scope.send = function(){
		$http.get('data/code.json').then(function successCallback(res){
			if(1 === res.data.state){
				count = 60;
				$scope.time = '60s';
				var interval = $interval(function(){
					if(count <=0){
						$interval.cancel(interval);
						$scope.time = '';
					}else{
						count --;
						$scope.time = count + 's';
					}
				},1000);
			}
		});
	}
}]);

以下是浏览器报错截图

https://img1.sycdn.imooc.com//szimg/5a31f0e8000185f906390030.jpg

https://img1.sycdn.imooc.com//szimg/5a31f0e80001de5405900362.jpg

https://img1.sycdn.imooc.com//szimg/5a31f0e800015acb17940751.jpg

我已经用的是then的方法发起请求的,不明白哪里出了问题,望老师看下,谢谢1

正在回答

2回答

页面功能正常吗?

好像这是validation模块和angular1.6才会出现的错误~可能不是你的代码问题

0 回复 有任何疑惑可以回复我~
  • 提问者 星空下的仓颉 #1
    测试了下,功能正常,我的angularjs是1.6版本的,是不是这个影响的哇~
    回复 有任何疑惑可以回复我~ 2017-12-15 14:01:07
提问者 星空下的仓颉 2017-12-15 14:04:47

然后纠结这个部分:

httpjs中:   get(url).then这里我用的是then方法,

'use strict';
angular.module('app').config(['$provide', function($provide){
  $provide.decorator('$http', ['$delegate', '$q', function($delegate, $q){
    $delegate.post = function(url, data, config) {
      var def = $q.defer();
      $delegate.get(url).then(function(res){
        def.resolve(res);
      },function(err){
        def.reject(err);
      });
      return {
        success: function(cb){
          def.promise.then(cb);
        },
        error: function(cb) {
          def.promise.then(null, cb);
        }
      }
    }
    return $delegate;
  }]);
}]);

registerjs中我用的是success方法,我改成then方法会报错,1.6版本的不是可以支持then方法废除success和erro方法了吗?这里为何必须用成success才可以,望老师抽空解答下,感谢!

'use strict';
angular.module('app').controller('registerCtrl',['$interval','$http','$scope','$state',function($interval,$http,$scope,$state){
	$scope.submit = function(){
    $http.post('data/regist.json',$scope.user).success(function(res){
        console.log(res.data);
    });
	}
	var count = 60;
	$scope.send = function(){
		$http.get('data/code.json').then(function successCallback(res){
			if(1 === res.data.state){
				count = 60;
				$scope.time = '60s';
				var interval = $interval(function(){
					if(count <=0){
						$interval.cancel(interval);
						$scope.time = '';
					}else{
						count --;
						$scope.time = count + 's';
					}
				},1000);
			}
		});
	}
}]);


0 回复 有任何疑惑可以回复我~
  • 提问者 星空下的仓颉 #1
    我说的success值这里
    
    $http.post('data/regist.json',$scope.user).success(function(res){
            console.log(res.data);
        });
    回复 有任何疑惑可以回复我~ 2017-12-15 14:06:01
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信