I have a jQuery ajax call that looks like this:
$.ajax({
type : 'GET',
dataType : 'json',
url : '/Zoo/animals',
success : function(data){
var count = data.length;
for(var i = 0; i < count; i++) {
${'#tableBody').append('<tr><td>');
${'#tableBody').append('<a href="@{Animal.index(data[i].name)}">' + data[i].name + '</a>');
${'#tableBody').append('</td></tr>');
}
}
})
Now all of this works fine except for the href attribute. Play! gives me the following error:
Exception raised was NullPointerException : Cannot get property 'name' on null object.
The json string that gets returned from the ajax call is fine as the text of the link shows up as the name of the animal as expected. I would like to pass the name of the animal to the Animal.index action however have been unable to do so.
Update:
As others have mentioned, I have also tried this:
${'#tableBody').append('<a href="@{Animal.index(' + data[i].name + ')}">' + data[i].name + '</a>');
which just results in the link of:
localhost:9000/animal/index?name=+%2B+data[i].name+%2B+
The problem is that Play! generates the url when the file is served, well before any javascript is executed. No change to the string or quotes here or there is going to help you.
A workaround:
This allows Play! to generate its url at “compile” time, and then javascript simply executes a replace at “run” time. Not sure if Play! will look for a variable named XXX, if so, add a dumby variable named XXX with value “XXX” on the server side.
UPDATE
I don’t have a Play! instance in front of me to fiddle with, but instead of a variable, you could probably also insert the string directly as a value by quoting the XXX:
And then you wouldn’t need a variable named XXX. The details require a bit of playing around, although the idea is the same.