2017-08-03 60 views
0

我的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" 
 
    } 
 
}

回答

0

您的then功能的錯字。錯誤回調在參數列表之外。相反,它應該是這樣的:

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

我只是從5日線的錯誤回調函數回來後移動)

應該這樣做。我建議您將第二個請求從$.ajax更改爲使用$http服務,以保持代碼的一致性。

+1

非常感謝,節省了我一天的時間 – hamed