HTTP 请求未在刷新页面中执行



我的 http 请求在第一次刷新时我的令牌过期后不起作用,但在第二次刷新中工作正常,但函数执行正常,做错了什么。 我在棱角框架中工作

$http({
url: "http://localhost/ArisSystem/api/system/subsystem",
method: "GET",
dataType: 'json',
ContentType: 'application/x-www-form-urlencoded',
params: {
parentId: $scope.systemIdToLoad
},
headers: authHeaders
}).then(function (response) {
$scope.subsystem = response.data;
}), function (xhr, status, error) {
if (refreshtoken && xhr.status === 401) {
$scope.refreshlocal();
}
}
$scope.refreshlocal = function () {
$.ajax({
url: "http://localhost/ArisSystem/login",
data: {
refresh_token: refreshtoken,
grant_type: 'refresh_token'
},
type: 'POST',
dataType: 'json',
ContentType: 'application/x-www-form-urlencoded',
success: AjaxSucceeded,
error: AjaxFailed
})
function AjaxSucceeded(response) {
localStorage.setItem('accessToken', response.access_token);
localStorage.setItem('refreshToken', response.refresh_token);
refreshtoken = localStorage.getItem('refreshToken');
accesstoken = localStorage.getItem('accessToken');
authHeaders.Authorization = 'Bearer ' + accesstoken;
}
function AjaxFailed(err, response) {
window.location.href = "login.html"
}
}

您的then函数中有拼写错误。错误回调位于参数列表之外。相反,它应该是这样的:

$http({
...
}).then(function (response) {
$scope.subsystem = response.data;
}, function (xhr, status, error) {
if (refreshtoken && xhr.status === 401) {
$scope.refreshlocal();
}
});

我只是在错误回调函数之后将)从第 5 行移回。

这应该做到。我建议您更改$.ajax的第二个请求以使用$http服务,只是为了保持代码一致。

最新更新