Possibly unhandled rejection: error

来源:6-5 用户模块逻辑编写-使用表单校验

星空下的仓颉

2017-12-14

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

这是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);
			}
		});
	}
}]);

以下是浏览器报错截图

http://img.mukewang.com/szimg/5a31f0e8000185f906390030.jpg

http://img.mukewang.com/szimg/5a31f0e80001de5405900362.jpg

http://img.mukewang.com/szimg/5a31f0e800015acb17940751.jpg

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

写回答

2回答

慕雪1613582

2017-12-14

页面功能正常吗?

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

0
1
星空下的仓颉
测试了下,功能正常,我的angularjs是1.6版本的,是不是这个影响的哇~
2017-12-15
共1条回复

星空下的仓颉

提问者

2017-12-15

然后纠结这个部分:

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
共1条回复

Angular JS 仿拉勾网 WebApp 开发移动端单页应用

基于AngularJS,仿拉勾网开发一个招聘类的移动端单页应用

1660 学习 · 857 问题

查看课程