Skip to content

Example Of a Secure Ajax Call Using Play, Scala and JQuery

Mike Slinn edited this page Jul 3, 2013 · 1 revision

Here is an Ajax call:

$.ajax({
    url: jsRoutes.controllers.Application.previousRecordings().url,
    type: "POST",
    dataType: "json",
    data: { questions: "Why is there air?" }
  }).done(function(childNodes) {
    alert("TODO do something")
  }).fail(function(jqXHR, textStatus, errorThrown){
    alert(textStatus + ", " + errorThrown);
  });

...that uses this routing:

GET  /javascriptRoutes   controllers.Application.javascriptRoutes
POST /previousRecordings controllers.Application.previousRecordings()
GET  /search/:input      controllers.Application.search(input)

...and these controller methods:

def javascriptRoutes = Action { implicit request =>
  import routes.javascript._
  Ok(Routes.javascriptRouter("jsRoutes")(
     routes.javascript.ApplicationController.previousRecordings,
     routes.javascript.ApplicationController.search
  )).as("text/javascript")
}

def previousRecordings() = SecuredAction(ajaxCall=true) { implicit secureRequest =>
  println("previousRecordings handler responded")
  Ok
}

def search(input: String) = SecuredAction(ajaxCall=true) { implicit request =>
  val result = FullTextSearch(input, request.user).search
  Ok(result)
}
Clone this wiki locally