Reputation: 4335
I just implemented the Google OAuth hybrid server side flow which requires an AJAX call for retrieving an access token. After I did the AJAX call (stored the token etc.) I would like the server to display some content using a file called home.scala.html.
//called when client has confirmed the OAuth Login dialog
function signInCallback(authResult) {
if (authResult['code']) {
$.ajax({
url: "http://localhost:9000/storeauthcode",
...
success: function(result) {
//access token was saved
//now redirect to the home area of the website
window.location.replace("http://localhost:9000/home");
},
error: function (xhr, status, error) {
//handle error appropriately here
...
}
});
}
}
However, if I get the token and everything works fine, I see the template code of my home.scala.html file (so it prints the html markup) but does not interpret this as Html that should be rendered by the browser.
How can I make the browser render the home.scala.html file as an Html file and not as plain text?
PS: Does it even make sense to do a redirect here or should the content be displayed on the same page? Looks a little bit weird when the Google login window is closed and the user has to wait until he is redirected to the actual site.
How should I get this content (AJAX, AngularJS)?
Upvotes: 0
Views: 588
Reputation: 150
Play generates Javascript code to handle routing from client-side code back to your application. That is called Javascript Routing.
First you create a router:
public static Result javascriptRoutes() {
response().setContentType("text/javascript");
return ok(Routes.javascriptRouter("jsRoutes",
controllers.routes.javascript.Users.storeAuthCode()));
}
then add it to your routes file:
GET /javascriptRoutes controllers.Application.javascriptRoutes
and to your HTML:
<script type="text/javascript" src="@routes.Application.javascriptRoutes"></script>
Last but not least, the code to invoke it would look something like this:
$.ajax(jsRoutes.controllers.Users.storeAuthCode())
.done( /*...*/ )
.fail( /*...*/ );
Upvotes: 1
Reputation: 150
You should declare the argument as type Html
in home.scala.html
. Your code would look something like this:
@(title: String)(code: Html)
<head>
<title>@title</title>
</head>
<body>
@code
</body>
Upvotes: 0