Handle HTTP 302 response from proxy in angularjs

前端 未结 5 1555
迷失自我
迷失自我 2020-11-28 06:19

I have a reverse proxy that checks global authentication for several applications. When the user is disconnected but still trying to use my application, the proxy sends a 30

5条回答
  •  一生所求
    2020-11-28 06:50

    I had a very similar issue, and considered the solution provided by Ofer Segev, but checking the content of the response to see if it matched an html fragment of another page just seemed too hacky to me. What happens when someone changes that page?

    Fortunately, I had control of the backend as well, so instead of returning a 302 (Redirect), I returned a 403 (Forbidden), and passed the desired location in the headers. Unlike the 302, the 403 will be handled in your error handler, where you can decide what to do next. Here was my resulting handler:

    function ($scope, $http) {
        $http.get(_localAPIURL).then(function (response) {
                // do what I'd normally do
            }, function (response) {
            if (response.status == 403)
            {
                window.location.href = response.headers().location;
            }
            else
            {
                // handle the error
            }
    

提交回复
热议问题