javascript - Asynchronous call in if statement ends up executing else statement -
this question has answer here:
- how return response asynchronous call? 24 answers
in specific case execute asynchronous call inside if
block, how come else
statement block gets executed if dosubmit
evaluates true
? outcome of ends @ line after error_2
comment:
$scope.save = function() { if (dosubmit) { var dto = { 'attributes': $scope.fields, 'requestorigin': $location.absurl() }; var req = { method: 'post', url: 'endpoint', data: dto }; $http(req).success(function(data, status) { // success $scope.completed(data); }).error(function(data, status) { // error_1 $scope.validationfailed(); }); } else { // error_2 $scope.validationfailed(); } } // used provide error messages input validation $scope.validationfailed = function(message, id) { $scope.alerts.push({ type: 'danger', msg: message || 'error.validation_failed', id: id || 'general' }); }
i read case somewhere can't remember source. link or short elaboration helpful.
addition 1 $scope.validationfailed
my comment answer:
maybe it's $http
error callback $scope.validationfailed();
executed? try adding alert(1);
in error callback.
this might indicate server side problem, causing server return status code different 200 both else
statement , error callback executing same function, show else statement not 1 being executed.
Comments
Post a Comment