如何使用jQuery在.ajax帖子中发送数组?

时间:2010-03-08 17:29:18

标签: jquery ajax json post

我已经通过简单的收集数据循环并将其推送到数组中。然后我尝试将该数组发送到页面方法(.aspx)。关于阵列的一些东西我认为不喜欢。这是我的代码:

//packaging table data for submit to server
            $("#saveToDB").click(function() {
                var dataForSubmit = new Array();
                //gather all data to array except the "delete" cell, .rowToDelete
                $('#QueueTable tbody td:not(.rowToDelete)').each(function() {
                    dataForSubmit.push($(this).html());

                });
                //test the array
                //alert(dataForSubmit);

                //send array to method
                $.ajax({
                    type: "POST",
                    url: "DailyReceipts.aspx/saveData",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: dataForSubmit,
                    success: function(msg) {
                        $.jGrowl('Your data has been successfully saved.', { header: 'Important' });
                        $('#result').append(msg.d)
                    },
                    error: function() {
                        $.jGrowl('An error has occured in saving the data.', { header: 'Important' });
                    }
                });
            });

2 个答案:

答案 0 :(得分:4)

只需在数据参数上添加它所期望的任何参数,如下所示:

data: "paramName=" + dataForSubmit,

或者,或者:

data: { paramName : dataForSubmit },

答案 1 :(得分:0)

你应该使用这个函数:JSON.stringfy,所以:

//send array to method
                $.ajax({
                    type: "POST",
                    url: "DailyReceipts.aspx/saveData",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringfy(dataForSubmit),
                    success: function(msg) {
                        $.jGrowl('Your data has been successfully saved.', { header: 'Important' });
                        $('#result').append(msg.d)
                    },
                    error: function() {
                        $.jGrowl('An error has occured in saving the data.', { header: 'Important' });
                    }
                });