1

My RestEasy code dispatches a 303-response to angularjs controller. Furthermore, Chrome network debugging shows that both the HTTP 303 is received AND that the new URL is loaded (showing as HTTP 200) - but never shown!

If I try calling the RestEasy-endpoint directly from my browser, it will redirect nicely!

But, alas, my angularjs controller seems unable to update the browser - it stays put on the same page!

Here is my angularjs controller:

var mod = angular.module('login', []);
mod.controller('RestController', [
        '$http',
        '$scope',
        function($http, $scope) {

            $scope.submit = function(v1, v2) {
              $http.post("rest/login/submit", {
                  param1 : v1,
                  param2    : v2
                }).success(function(data, status, headers, config) {
                    $scope.submitmsg = "";

            }).error(function(data, status, headers, config) {
                $scope.submitmsg = "Error...";
            });
        }
    } ]);

My question is simple: should I do something special to handle update of the browser page. Perhaps set some $scope variable or $doc or something?? Or should I maybe do some handling in the error()-part of the function - to address the 303 code?

Any help is appreciated. All testing has been done in Chrome.

mortensi
  • 2,907
  • 1
  • 13
  • 11
  • Why u want to return 303 code on ajax requests - return data like {itemCreatedId: '3837373'} and in success go to #viewItem/3837373. – Petr Averyanov Dec 11 '14 at 18:43
  • It's not that I absolutely want to perform a redirect in this particular way - this is simply what "came to me" since I had the RestEasy endpoint and an angularjs controller. If you know of an easier way to do a redirect, I will be happy to receive your advice. – mortensi Dec 12 '14 at 13:59

2 Answers2

1

The 303 response code should not really be used in single page applications. It's processed by the browser before the angular framework.

You should return the appropriate error code instead of the 3xx redirected command; 5xx in case of server error, 401 when unauthorised and so forth.

If this should be a global exception hander or just in the controller specific, really depends of action that should be taken because of it. In the light of the information available, I would recommend an global hander to the application which would redirect the user using the $location.path("...");

Heikki
  • 199
  • 5
  • Thanks for your comment, it helped me to realize I was searching for a solution in the wrong place. It seems to be a bad idea to send 303 from the Rest endpoint. I decided to send a HTTP 200 instead (along with some url-information). However, using $loaction.path() seems to be the wrong choice to actually perform a redirect. In the end, $window.location.href did the trick for me. I will post an answer on that, and comments are of course welcome. – mortensi Dec 13 '14 at 09:03
  • @mortensi $location service is not the wrong choice to perform the browser redirect. If you are using the lower level API (window.location) beware of the things you are *giving up*. Have a look at the section "Comparing $location to window.location" from https://docs.angularjs.org/guide/$location for clarity. – Heikki Dec 17 '14 at 13:41
  • According to the upvoted answer on http://stackoverflow.com/questions/18875467/redirect-to-new-page-in-angularjs-using-location, $location is not the way to go. There could be something I don't understand here, so please clarify if possible. – mortensi Dec 18 '14 at 10:08
  • @mortensi My apologies, I didn't realise that you want to navigate outside of your application. I that case you are correct, using lower level API is your only option. – Heikki Dec 18 '14 at 20:04
1

It is not a good idea to send HTTP 303 from your endpoint. I think it may be intercepted by the browser. A better alternative is to return information about the URL you want to redirect to (along with an appropriate HTTP response - could be 200 or 400 or whatever) and use the $window.location.href to perform the redirect:

var mod = angular.module('login', []);
mod.controller('RestController', [
        '$http',
        '$scope',
        '$window',
        function($http, $scope,$window) {

            $scope.submit = function(v1, v2) {
              $http.post("rest/login/submit", {
                  param1      : v1,
                  param2    : v2
                }).success(function(data, status, headers, config) {
                    $window.location.href = "http://host:port/path"
                }).error(function(data, status, headers, config) {
                    $scope.submitmsg = "error";
                });
            }
        } ]);
mortensi
  • 2,907
  • 1
  • 13
  • 11