Sirius XIV
Sirius XIV

Reputation: 1

Using Ajax load method with jQuery on Play 2 doesn't work

I am using the Play 2.0 framework. I am trying to update a division in a template page using jQuery every 500ms, but it doesn't work. I've already looked here : http://www.objectify.be/wordpress/?p=428 . I am pretty sure that I followed accurately all the instructions.

Here is the tricky part of my template :

<body onLoad="testFin()">
<div id="date_fermeture"></div>
<script src="@routes.Assets.at("/javascripts/jquery-1.10.2.min.js")"></script>
<script>
function termine(){
    $('#date_fermeture').load('/eleve/infoheure/@serie.id');
}
function testFin(){
    setInterval("termine();",500);
}
</script>
</body>

This is my java code :

public static Result infoHeure(Long serie_id){
 Serie serie = Serie.find.ref(serie_id);
 return ok(infoHeure.render(serie.date_fermeture));
}

This is the route :

 POST   /eleve/infoheure/:serie_id  controllers.Application.infoHeure(serie_id: Long)

And this is the template infoHeure.scala.html :

@(date_fermeture : Date){
<div id="date_fermeture">
@if(date_fermeture!=null){
   @date_fermeture.getTime()
}else{
   Not finished. The date is not yet determined.
}
</div>
}

I am pretty sure the "testFin" function works and call the "termine" function every 500ms. My "serie" class contains various things and among them is the date I want to access. The code compiles without problem, but the page doesn't display the date as it should.

The browser console from Firefox doesn't give any error.

Thank you for your help.

Upvotes: 0

Views: 235

Answers (1)

mguillermin
mguillermin

Reputation: 4181

Your route is defined with a POST method and the jQuery load() is performing a GET request by default (if you don't provide POST data as a second parameter).

You can try to call directly the /eleve/infoheure/... URL in your browser, it will perform a GET request and you will get back a 404 error because Play can't find a matching route.

You have two solutions: change your route to use a GET instead of a POST or modify your javascript call to make jQuery perform a POST.

In your case, assuming that your infoHeure() action doesn't do any modification on your data, it's better to change your route to use a GET method.

Upvotes: 1

Related Questions