gpt4 book ai didi

javascript - 非 200 时 Angular 没有得到响应

转载 作者:行者123 更新时间:2023-11-30 17:21:52 27 4
gpt4 key购买 nike

我已经发现了 httpInterceptor here .

如果我的 Basic Auth header 具有有效凭据,则一切正常,但如果我返回 401,则应用程序会挂起,我再也不会收到响应。

这是我的拦截器:

angular.module('TDE').factory('httpInterceptor', function httpInterceptor ($q, $window, $location) {
return function (promise) {
var success = function (response) {
//window.logger.logIt("httpInterceptor received a good response: " + response);
return response;
};

var error = function (response) {
//window.logger.logIt("httpInterceptor received an error: " + response);

if (response.status === 401) {
$location.url('/login');
}

return $q.reject(response);
};

return promise.then(success, error);
};
});

在 app.js 中声明 httpInterceptor

angular.module('TDE', []);

var TDE = angular.module('TDE', ['ux', 'ngRoute', 'ngResource', 'TDE', 'hmTouchEvents', 'infinite-scroll', 'ui.bootstrap', 'ui.sortable']);

TDE.config(['$routeProvider', '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
$httpProvider.responseInterceptors.push('httpInterceptor');

$routeProvider
.when('/', {})
.when('/login', { templateUrl: "Views/Login/login.html", controller: "LoginController" })

还有我的身份验证方法

authenticate: function (user, password) {
// window.logger.logIt("serviceAccount: " + $rootScope.serviceAccount);
window.logger.logIt("In authenticate...");

var deferred = $q.defer();
var encoded = encoder.encode($rootScope.serviceAccount);
//var encoded = encoder.encode(user + ":" + password);

if (user && password) {
window.logger.logIt("We've got a username and password...");

$http.defaults.headers.common.Authorization = 'Basic ' + encoded;

sessionStorage.setItem('Authorization', $http.defaults.headers.common.Authorization);

var url = $rootScope.serviceBaseUrl + "login/authenticate";

window.logger.logIt(url);

$http({
method: "POST",
url: url,
data: {
"Username": user,
"Password": password,
"AccessToken": ""
},
headers: {
"Content-Type": "application/json"
}
})
.success(function (data, status, headers, config) {
window.logger.logIt("We've got a response (success)...");

if (data.IsAuthenticated) {
deferred.resolve(data);
session.setSession();
} else {
deferred.reject(status);
}
})
.error(function (data, status, headers, config) {
window.logger.logIt("We've got a response (error)...");

$dialogservice.showMessage("Authentication Error", "Return Code: " + status);
deferred.reject(status);
});
} else {
window.logger.logIt("We've got a response...");

deferred.reject(401);
}

return deferred.promise;
},

您会看到在我的 Authenticate 方法中,我正在测试两行:

var encoded = encoder.encode($rootScope.serviceAccount);

var encoded = encoder.encode(user + ":" + password);

我们需要使用基本身份验证(通过 SSL)。现在,我正在测试的是我能否收到 401 回复。如果我使用 $rootScope.serviceAccount(正在运行),我会立即收到 200 响应。但是,如果我故意发送错误的用户名/密码,我永远不会收到回复,应用程序就在那里。

编辑:好的,我已将我的代码更新为以下内容,但仍然出现相同的行为:

angular
.module('TDE')
.config(['$httpProvider', function ($httpProvider) {

$httpProvider.interceptors.push(function ($q) {
return {
'request': function (config) {
window.logger.logIt("Request is being sent...");

var headers = config.headers;

if (!headers.Authorization) {
headers.Authorization = sessionStorage.getItem('Authorization');
}

return config || $q.when(config);
},

'response': function (response) {
window.logger.logIt("got a good response...");

return response;
},

'responseError': function (rejection) {
window.logger.logIt("responseError error...");

return $q.reject(rejection);
},
};
});
}]);

最佳答案

好吧,PhoneGap 又是问题所在!!!!!!!!!!!!!!!!!!!!!

Example 1

Example 2

Example 3

Example 4

关于javascript - 非 200 时 Angular 没有得到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25017008/

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