问题描述
我通过Ajax的jQuery通过发送值的阵列,播放框架后端,和我在一个问题面前。
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 ...
}
});
但在玩!如果我做了 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
但玩!框架期望阵列数据要发送的的id = 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 code完好)刚刚宣布你的控制器的方法是这样的:
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]
这篇关于游戏框架和jQuery Ajax和数据阵列的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!