Skip to content

Instantly share code, notes, and snippets.

@aldavigdis
Created July 16, 2015 08:14
Show Gist options
  • Save aldavigdis/a52bc9edb7cedfcc0f75 to your computer and use it in GitHub Desktop.
Save aldavigdis/a52bc9edb7cedfcc0f75 to your computer and use it in GitHub Desktop.
Connecting with a remote server via XHR and handling connection errors
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