里面$就功能结果数组转换成字符串 [英] Function result array inside $.ajax is converted to string

查看:100
本文介绍了里面$就功能结果数组转换成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过使用$阿贾克斯数据变量ID的数组。阵列是一个函数的结果。如果我宣布这个功能的之外 $就它正确地发送数组。但是,如果我把同样的功能code $阿贾克斯(这对我来说是preffered),我把它作为一个字符串。

I need to pass array of ids with $.ajax data variable. The array is a result of a function. If i declare this function outside $.ajax it sends array correctly. But if i put same function code inside $.ajax (which is preffered for me), i get it as a string.

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;
        }
    }
});

下面是小提琴: http://jsfiddle.net/vxLrN/7/

您可以看到,第一次请求与发送订购作为数组,而二传订购字符串,虽然功能是绝对平等的。

You can see that first request is sent with ordering as an array, while second pass ordering as string, although, functions are absolutely equal.

我怎么可以把函数内联,仍然可以得到阵列的结果?
谢谢

How can i put function inline and still get array result? Thanks

推荐答案

那么请确保您以分配正确的结果(字符串数组)的订购参数:

Well make sure that you invoke this anonymous function in order to assign the proper result (array of strings) to the ordering parameter:

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
}

这篇关于里面$就功能结果数组转换成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆