使用 Play! 将 json 对象属性注入锚点的 href 属性中框架
我有一个 jQuery ajax 调用,如下所示:
$.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>');
}
}
})
现在,除了 href
属性之外,所有这些都可以正常工作。玩!给我以下错误:
Exception raised was NullPointerException : Cannot get property 'name' on null object.
从 ajax 调用返回的 json 字符串很好,因为链接的文本按预期显示为动物的名称。我想将动物的名称传递给 Animal.index
操作,但无法这样做。
更新:
正如其他人提到的,我也尝试过这个:
${'#tableBody').append('<a href="@{Animal.index(' + data[i].name + ')}">' + data[i].name + '</a>');
这只会产生以下链接:
localhost:9000/animal/index?name=+%2B+data[i].name+%2B+
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+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是玩!在提供文件时(在执行任何 javascript 之前)生成 url。对这里或那里的字符串或引号进行任何更改都不会对您有帮助。
解决方法:
这允许播放!在“编译”时生成其 url,然后 javascript 在“运行”时简单地执行替换。不确定是否可以玩!将查找名为 XXX 的变量,如果是,则在服务器端添加一个名为 XXX 且值为“XXX”的哑变量。
更新
我没有玩!我前面的实例可以摆弄,但除了变量之外,您还可以通过引用 XXX: 直接将字符串作为值插入:
然后您就不需要名为 XXX 的变量。尽管想法是相同的,但细节需要一些尝试。
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.
这听起来像是
jsAction
标签的工作。它生成一个 javascript 函数,其工作原理与 davin 的答案相同。您可以将 var AnimalAction = #{jsAction @Animals.index(':name') /}; 放在 ajax 调用之前的某个位置,然后将链接生成代码更改为:
您会注意到生成的函数接受一个对象作为参数,因此它可以支持任意数量的参数。
文档中没有明确说明的是标签 (
:name
) 必须与相应控制器操作中的参数名称相同 (public static void index(String name )
),当然必须与routes
文件中指定的方式相同 (GET /animals/{name} Animals.index
)。This sounds like a job for the
jsAction
tag. It generates a javascript function which works along the same lines as davin's answer.You would put
var animalAction = #{jsAction @Animals.index(':name') /};
somewhere before your ajax call, then change your link generation code to:You'll notice the generated function takes an object as it's argument, so it can support as many parameters as you want.
What isn't explicitly clear in the docs is that the label (
:name
) must be the same as the name of the parameter in the corresponding controller action (public static void index(String name)
), which of course must be the same as how it's specified in theroutes
file (GET /animals/{name} Animals.index
).data 是一个 javascript 对象,您需要指定类似于打印链接文本的方式。你能试试这个吗?
data is a javascript object, you need to specify that similar to how you are printing the text of the link. Can you try this?
您正在循环
data
数组,但需要通过索引i
引用每个数据对象,就像data[i]
一样。我会尝试这个:这将替换成功回调中的所有内容
You are looping over
data
array but you need to reference each data object by the indexi
so likedata[i]
. I would try this:This will replace everything inside your success callback
你应该改变这一行 -
到这个 -
you should change this line-
to this-