Nishan
Nishan

Reputation: 365

Redirect issue from localhost using Play Framework & Angularjs

I am using Play Framework and Angularjs for my application. My requirement is to redirect Play framework page based on some parameter selected on UI.

Here is my code,

On Page I opened bootstrap modal dialog and on submit action calling angular post method,

$scope.loginToExt = function() {

  var data = $scope.env;

  $http.post('/login', {env: data})
    .success(function(data){
      //$scope.env = data
    })
    .error(function(data){
      console.log(data)
    });
};

Once I click submit on my modal it will call '/login' and based on my route mapping will call action method as below,

def login = Action(parse.json) { implicit request =>
val env = (request.body \ "env").as[String]

env match {
  case _ => {
    Redirect("https://google.com")
      .withHeaders(ACCESS_CONTROL_ALLOW_ORIGIN -> "*")
  }
}

Route file as below,

POST     /login             controllers.Application.login

I don't know what the issue is but I was getting error as below and unable to redirect my page.

XMLHttpRequest cannot load https://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

Upvotes: 1

Views: 1146

Answers (2)

Arpit Suthar
Arpit Suthar

Reputation: 754

By looking https://gist.github.com/mitchwongho/78cf2ae0276847c9d332 , You have to add "access-control-allow-origin" in HeaderNames.ACCESS_CONTROL_ALLOW_HEADERS in Global.scala file.

Upvotes: 0

BatteryAcid
BatteryAcid

Reputation: 8901

I believe the issue you are having is because you are trying to redirect inside an ajax ($http.post) call, see this post:

Your server side code cannot redirect the browser from an ajax response.

Using $http.post and res.redirect without resolving the promise

See also: Handle an express redirect from Angular POST

Instead of trying to redirect from Play, consider returning a response with the url of the redirect, then in your success call back, redirect from there:

$window.location.href = "https://google.com";

(remember to inject $window)

Upvotes: 1

Related Questions