Skip to content

Instantly share code, notes, and snippets.

@songfei1983
Last active August 29, 2015 14:17
Show Gist options
  • Save songfei1983/1198937f83f42c50bef2 to your computer and use it in GitHub Desktop.
Save songfei1983/1198937f83f42c50bef2 to your computer and use it in GitHub Desktop.
Play Framework 2.3.x Ajax通信 ref: http://qiita.com/songfei1983/items/b3540794b0664e089776
/**
* This action is used to serve Home page of the application
*
* @return
*/
def index = Action { implicit request =>
Ok(views.html.index("Your new application is ready."))
}
/**
* This action is used to handle Ajax request
*
* @return
*/
def ajaxCall = Action { implicit request =>
Ok("Ajax Call!")
}
$(function() {
ajaxCall();
});
var ajaxCall = function() {
var ajaxCallBack = {
success : onSuccess,
error : onError
}
jsRoutes.controllers.Application.ajaxCall().ajax(ajaxCallBack);
};
var onSuccess = function(data) {
alert(data);
}
var onError = function(error) {
alert(error);
}
@(message: String)(implicit req: RequestHeader)
@main("Welcome to Play") {
@message
}
@(title: String)(content: Html)(implicit req: RequestHeader)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<!-- Embedded Javascript router -->
@helper.javascriptRouter("jsRoutes")(
routes.javascript.Application.index,
routes.javascript.Application.ajaxCall
)
<script src="@routes.Assets.at("javascripts/app.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>
GET /ajax-call controllers.Application.ajaxCall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment