0

I am using Play 2 with Java and one of my controller methods returns a redirect:

return redirect(<some other domain>);

The client side call happens from an angular controller through $http:

$http.get("/signin").
    ...

This does not work; Firefox tells me to enable CORS. So I tried to enable CORS as suggested by the answers to this StackOverflow question. But I still get the same error. However that answer seems to be directed towards JSON responses. Do I need to do anything different for a redirect?

I would have thought that setting Access-Control-Allow-Origin to * would do the trick, but that doesn't seem to work.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
  • Could you please tell something more about the way you execute this redirect? CORS is connected with AJAX. Simple redirect should work just fine with an ordinary http call. – Daniel Olszewski Sep 09 '14 at 12:52
  • The controller method is issuing this call: `return redirect("https://api.twitter.com/oauth/authenticate?oauth_token=" + requestToken.getToken());` – Jayanth Koushik Sep 09 '14 at 13:03
  • I was rather asking about how do you call the action which return that redirect? – Daniel Olszewski Sep 09 '14 at 13:34

1 Answers1

1

Http 3xx redirection responses are transparent to AJAX calls. One possible solution for this problem is to return something else than 303 which can be resolved by AJAX. For example you can assume that all responses from your application with code 280 are intended for an AJAX redirection. Then your controller would look like this:

public class Application extends Controller {

    public static Result signin() {
        // ...
        return status(280, "https://api.twitter.com/oauth/authenticate?oauth_token=" + requestToken.getToken());
    }

}

On the client side you could check a result status code and react for code 280. Below there's a simple example with a page redirect but you can do anything you like with that response.

<script>
    $(function() {
        $.ajax({'url': '/signin', statusCode: {
            280: function(response) {
                window.location = response;
            }
        }});
    });
</script>
Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65