Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jQuery,如何将字符串数组作为http参数附加到http请求?_Java_Jquery_Arrays_Spring Mvc_Httprequest - Fatal编程技术网

Java 使用jQuery,如何将字符串数组作为http参数附加到http请求?

Java 使用jQuery,如何将字符串数组作为http参数附加到http请求?,java,jquery,arrays,spring-mvc,httprequest,Java,Jquery,Arrays,Spring Mvc,Httprequest,我有一个spring控制器,其请求映射如下 @RequestMapping("/downloadSelected") public void downloadSelected(@RequestParam String[] ids) { // retrieve the file and write it to the http response outputstream } 我有一个对象的html表,每一行都有一个复选框,该复选框的值是对象的id。当他们提交时,我有一个jQuery回调来

我有一个spring控制器,其请求映射如下

@RequestMapping("/downloadSelected")
public void downloadSelected(@RequestParam String[] ids) {
    // retrieve the file and write it to the http response outputstream
}
我有一个对象的html表,每一行都有一个复选框,该复选框的值是对象的id。当他们提交时,我有一个jQuery回调来序列化所有ID。我想将这些ID粘贴到一个名为“ids”的http请求参数中,以便轻松地获取它们

我想我可以做到以下几点

var ids = $("#downloadall").serializeArray();
然后我需要获取每个ID,并将它们添加到名为ids的请求参数中。但有没有一种“标准”的方法来做到这一点?比如使用jQuery?

我不知道“标准方式”,但我会这样做

var ids = $("#downloadall").serializeArray();
将在表单上为您提供数据集(仅显示选中的项):

要将其提供给jQuery的.ajax(),只需执行以下操作:

$.ajax({
网址:,
数据:ids.map(函数(i){returni.name+'='+i.value;}).join('&'))
});
Array.map()还不能与所有浏览器兼容,因此您最好在页面上也包含以下代码:

if (!Array.prototype.map) {
  Array.prototype.map = function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }
    return res;
  };
}
if(!Array.prototype.map){
Array.prototype.map=函数(fun/*,thisp*/){
var len=this.length>>>0;
如果(乐趣的类型!=“功能”)
抛出新的TypeError();
var res=新阵列(len);
var thisp=参数[1];
对于(变量i=0;i
我从中得到的代码片段


我并没有将它们放在
?ids=…
参数中,但通过这种方式,它们很容易在服务器端访问。您可以随时根据需要修改地图功能。

是的,这也是个好主意!我喜欢jQuery,它们似乎总是满足您的需要:)
$.ajax({
  url: <your url>,
  data: ids.map(function (i) {return i.name+'='+i.value;}).join('&')
});
if (!Array.prototype.map) {
  Array.prototype.map = function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }
    return res;
  };
}