Play Framework 和 jQuery Ajax 请求,数据为数组 [英] Play Framework and jQuery Ajax request with data as Array
问题描述
我通过带有 Play Framework 后端的 jQuery 通过 Ajax 发送一组值,但我遇到了问题.
I am sending an Array of values through Ajax via jQuery with a Play Framework backend, and I'm in front of a problem.
这是一个例子:
$.ajax ({
'type': 'POST',
'url': '/url',
'timeout': 5000,
'data': {'ids': [0, 1, 2, 3]},
'dataType': 'json',
'success': function (oData) {
// Process ...
}
});
但是在 Play! 中,如果我执行 params.get("ids");
,我得到一个空值,如果我执行 params.getAll("ids");
也是.
But in Play!, if I do a params.get("ids");
, I got an empty value, and if I do a params.getAll("ids");
also.
我知道问题出在哪里,jQuery 将数据发送为:ids[]=0&ids[]=1&ids[]=2&ids[]=3
但玩!框架期望将数组数据作为 ids=0&ids=1&ids=2&ids=3
I know where the problem is, jQuery send the data as : ids[]=0&ids[]=1&ids[]=2&ids[]=3
but Play! Framework expect array data to be sent as ids=0&ids=1&ids=2&ids=3
是否有正确的方法可以正确发送数据(或将数据作为数组获取到我的控制器中)?
Is there a proper way to send the data properly (or get the data as an array in my controller) ?
到目前为止,我设法让它简单地工作,但在javascript中手动将请求创建为字符串.
So far, I managed to make it works simply but creating the request as a String manually in javascript.
感谢您的帮助.
推荐答案
一种方法(保持 JavaScript 代码不变)只是像这样声明控制器方法:
One method (leaving your JavaScript code intact) is just declaring your controller method like this:
public static void myMethod(@As("ids[]:")List<Long> ids) {
System.out.println(ids.get(0));
}
...输出是您所期望的:
.. the output is what you expect:
[0, 1, 2, 3]
这篇关于Play Framework 和 jQuery Ajax 请求,数据为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!