gpt4 book ai didi

javascript - 在服务中完成工作后, Angular 服务未在 Controller 中返回成功

转载 作者:行者123 更新时间:2023-12-03 05:14:08 25 4
gpt4 key购买 nike

我正在调用服务内部的方法,当服务工作完成时,它应该将数据返回到 Controller ,并继续进一步工作,但在调用服务并生成数据后,该方法是 Controller 停止并在控制台中返回错误:

错误:

Error: d is undefined
sendOtp/<@http://xxxxxxx:8087/controller/resources/static/js/controller/mainController.js:71:14
processQueue@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:14634:28
scheduleProcessQueue/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:14650:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:15916:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:15727:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:16024:13
done@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:10511:36
completeRequest@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:10683:7
requestLoaded@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:10624:1

Controller :

app.controller('signController', ['$scope','signUpService','$location','$rootScope', function($scope, signUpService, $location, $rootScope) {
$scope.OTP='';

function sendOtp(pNumber, countryId){

if(pNumber!==null){
$rootScope.pNumber = pNumber;
signUpService.sendPhoneOtp(pNumber,countryId)
.then(
function(d) {
/*$location.path('/otp', false);*/
$scope.OTP = d.response.result.otp;
console.error('OTP SENT SUCCESSFULLY'+$scope.OTP);
alert($scope.OTP);

},
function(errResponse){
console.error('Error while fetching OPT of NUMBER');
}
);
}else{
alert("Number can not be empty");
}

}

服务内部的方法:

    function sendPhoneOtp(pNumber,countryId){
var deferred = $q.defer();
$http({
method: 'POST',
url: 'https://xxxxx.com/service/verify/phone',
data: {
phone: pNumber,
countryId: countryId
}
}).success(function(response){
deferred.resolve(response.data);

}).error(function(errResponse){
console.error('Error while OTP');
deferred.reject(errResponse);
});
return deferred.promise;
}

最佳答案

尝试将您的 sendPhoneOtp 函数更改为:

function sendPhoneOtp(pNumber, countryId) {
return $http({
method: 'POST',
url: 'https://xxxxx.com/service/verify/phone',
data: {
phone: pNumber,
countryId: countryId
}
}).then(function(response){
return response.data;
}).catch(function(errResponse) {
console.error('Error while OTP');
});
}

在已弃用的 success 函数中,没有 data 属性,您也不需要使用 $q 服务,因为 $ http 本身就是一个 promise

关于javascript - 在服务中完成工作后, Angular 服务未在 Controller 中返回成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com