Created
July 16, 2015 08:14
-
-
Save aldavigdis/a52bc9edb7cedfcc0f75 to your computer and use it in GitHub Desktop.
Connecting with a remote server via XHR and handling connection errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function authenticate(callback, error) { | |
resetErrors(); | |
angular.element('#login-form input[type=submit]').button('loading'); | |
$scope.authRequest = new XMLHttpRequest(); | |
$scope.authRequest.open('GET', 'http://lfo-prtg:81/odata/', true, 'UNRWA\\' + $scope.username, $scope.password); | |
$scope.authRequest.send(); | |
$scope.connectionTimer = $interval(function () { | |
$scope.authRequest.abort(); | |
$scope.errorConnection = true; | |
angular.element('#login-form input[type=submit]').button('reset'); | |
}, 2500); | |
$scope.authRequest.onError = function () { | |
$scope.errorHttp = true; | |
angular.element('#login-form input[type=submit]').button('reset'); | |
}; | |
$scope.authRequest.onload = function () { | |
angular.element('#login-form input[type=submit]').button('reset'); | |
$interval.cancel($scope.connectionTimer); | |
if (this.status >= 200 && this.status < 400) { | |
resetErrors(); | |
callback(); | |
} else if (this.status >= 401 && this.status <= 403) { | |
$scope.errorAuth = true; | |
} else { | |
$scope.errorServer = true; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment