Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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
Javascript $.ajax中的函数结果数组被转换为字符串_Javascript_Jquery_Ajax_Arrays - Fatal编程技术网

Javascript $.ajax中的函数结果数组被转换为字符串

Javascript $.ajax中的函数结果数组被转换为字符串,javascript,jquery,ajax,arrays,Javascript,Jquery,Ajax,Arrays,我需要用$.ajax数据变量传递ID数组。数组是函数的结果。如果我在$之外声明此函数,它将正确发送数组。但是,如果我将相同的函数代码放在$.ajax(这是我的首选)中,我会将其作为字符串得到 function mySort(){ // Do not pass hidden clones var items = []; $('#fp_parameters_list').children().each(function(){ if ($(this).is(':visi

我需要用$.ajax数据变量传递ID数组。数组是函数的结果。如果我在$之外声明此函数,它将正确发送数组。但是,如果我将相同的函数代码放在$.ajax(这是我的首选)中,我会将其作为字符串得到

function mySort(){ // Do not pass hidden clones
    var items = [];
    $('#fp_parameters_list').children().each(function(){
        if ($(this).is(':visible')) {         
            items.push($(this).attr('data-parameter-id'));
        }
    });
    return items;
}

// This gives correct ordering
$.ajax({
    url: '/echo/json/',
    type: 'post',
    dataType: 'json',
    data: {
        ordering: mySort()
    }
});


// This gives ordering as a string
$.ajax({
    url: '/echo/json/',
    type: 'post',
    dataType: 'json',
    data: {
        ordering: function(){ // Do not pass hidden clones
            var items = [];
            $('#fp_parameters_list').children().each(function(){
                if ($(this).is(':visible')) {         
                    items.push($(this).attr('data-parameter-id'));
                }
            });
            return items;
        }
    }
});
这是小提琴:

您可以看到,第一个请求是以数组的形式发送的,而第二个请求是以字符串的形式发送的,尽管函数是绝对相等的

如何将函数内联并仍然获得数组结果?
谢谢

请确保调用此匿名函数,以便将正确的结果(字符串数组)分配给
排序
参数:

data: {
    ordering: (function () { // Do not pass hidden clones
        var items = [];
        $('#fp_parameters_list').children().each(function() {
            if ($(this).is(':visible')) {
                 items.push($(this).attr('data-parameter-id'));
             }
         });
         return items;
    })(); // <!-- Here call the anonymous function to get its result
}
数据:{
排序:(函数(){//不传递隐藏克隆
var项目=[];
$('#fp_参数_列表').children().each(函数(){
如果($(this).is(':visible')){
items.push($(this.attr('data-parameter-id'));
}
});
退货项目;

})();//请确保调用此匿名函数,以便将正确的结果(字符串数组)分配给
ordering
参数:

data: {
    ordering: (function () { // Do not pass hidden clones
        var items = [];
        $('#fp_parameters_list').children().each(function() {
            if ($(this).is(':visible')) {
                 items.push($(this).attr('data-parameter-id'));
             }
         });
         return items;
    })(); // <!-- Here call the anonymous function to get its result
}
数据:{
排序:(函数(){//不传递隐藏克隆
var项目=[];
$('#fp_参数_列表').children().each(函数(){
如果($(this).is(':visible')){
items.push($(this.attr('data-parameter-id'));
}
});
退货项目;

})();//只需使用$.map直接构建数组即可

$.ajax({
  url: '/echo/json/',
  type: 'post',
  dataType: 'json',
  data: {
    ordering: $.map($('#fp_parameters_list').children(':visible'), function(el) {
                 return $(el).data('parameter-id');
              })
  }
});

只需使用$.map直接构建数组即可

$.ajax({
  url: '/echo/json/',
  type: 'post',
  dataType: 'json',
  data: {
    ordering: $.map($('#fp_parameters_list').children(':visible'), function(el) {
                 return $(el).data('parameter-id');
              })
  }
});

真的吗?你不是在执行函数,而是将其作为数据发送。真的吗?你不是在执行函数,而是将其作为数据发送。这甚至是更少的代码。虽然,我接受了Darin的回复,因为它更接近主题,但我想我会使用你的解决方案。谢谢,这是更少的代码。尽管,我接受了Darin的回复,因为我同意t更接近主题,但我想我会使用你的解决方案。谢谢