0

I can't seem to figure out how to perform ajax calls in Play. I've used other frameworks before and it was just a matter of placing something like remote="true" on the form.

How do you use ajax in Playframework? What am I doing something wrong here?


Code:

/**
 * AJAX Form
 */

@helper.form(action = routes.SomeCtrl.ajaxForm()) {
    <p>
        <input type="submit">
    </p>
}

/**
 * myview.scala.js
 */
@(implicit r: RequestHeader)

$(function() {
    alert("AJAX is working!");
})

/**
 * SomeCtrl ajaxForm def
 */
def ajaxForm() = Action { implicit request =>
    Ok(views.js.myfolder.myview()).as("text/javascript")
    // just testing it
}

/**
 * routes
 */
POST    /assets/javascripts/ajax-form.js controllers.SomeCtrl.ajaxForm()
goo
  • 2,230
  • 4
  • 32
  • 53

1 Answers1

0

if you want include the js the you have to

<script type="text/javascript" charset="utf-8" src="@routes.SomeCtrl.ajaxForm()"></script>

according to your code your js will act as a page, and the content written on your js will shown on web as it is written.
for ajax refrence you can check http://www.playframework.com/documentation/2.2.x/ScalaJavascriptRouting

and check this question Play 2.x: How to make an AJAX request with a common button

Community
  • 1
  • 1
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
  • 1
    Note: `javascriptRoutes` in the other question are nice, but not required, @Jon can just use common AJAX call from the jQuery directly to `/assets/javascripts/ajax-form.js` url – biesior Apr 12 '14 at 19:45