Search the site :

Call a java function from a javascript function [play framework]

There are many ways to pass data and acess data from a java code and a javascript functionwithin play framework.But i was in a condition where i was to exchange

data from a javascript to java code and from java code to javascript.I figured out

it to be done using simple ajax code.

my ajax code :

$(document).ready(function(){

$("#ourl").keyup(function() {

var surl=$("#ourl").val();

var listAction = #{jsAction @UrlGeter.genurl(':url') /}

$.get(listAction({url: surl}), function(data) {

$("#newurl").replaceWith(data);

});

});

});

you can see the jsAction object here is the action invoker which calls the java function from

UrlGeter controller class

now you need to create a java function genurl() into the controller UrlGeter as:

public static void genurl(String date)

{

render(date)

}

note: String is more compatible argument type

now create genurl.html

${date}

include your controller into routes of your application and you are ready to go

Comments powered by Disqus
Contents © 2014 Kaushik Gandhi - Designed and built using Nikola, and hosted on github
Creative Commons License BY-NC-SA