Ajax request to the controller

I am new to Play and will try very simple things with AJAX. Right now I want to send data to the controller and send something back. I don’t understand how to implement this in Play.

I used to send data using

$.get(url, {data:'input'), function() { do something });

to the standard servlet found in / url. In servlet I have a simple

out.println("html output")

if I want to print something in my html file. I hope you understand.

In Play, I have a function in my controller (this is nonsense, just a test ...)

public static void doIt(String input) {
    String out = input+"_foo";
    render(out);
}

I am trying to call this function using jQuery / AJAX as follows:

$(document).ready(function() {
      // when I click a button ...
      $("#send").click(function(){
            var url = #{jsAction @doIt(':input') /}     
            $.get(url({input: 'x'}), function() {
                ...
            });
      });
});

This is taken from a textbook and does not work. Can someone give me an idea on how to write a controller and JS to send some random string to my controller and return something.

Greetings

+3
source share
2

renderText() render():

public static void doIt(String input) {
    String out = input + "_foo";
    renderText(out);
}

, , ( script) :

$(document).ready(function() {
  // when I click a button ...
  $("#send").click(function(){
        var url = #{jsAction @YourController.doIt(':input') /}     
        $.get(url({input: 'x'}), function() {
            ...
        });
  });

});

. : , js

+5

http://www.playframework.org/documentation/1.1.1/ajax . : " ". , .
, - jsAction:

 #{jsAction @YourController.doIt(':input') /}  

!

+1

All Articles