如何使用jQuery在.ajaxpost中发送数组?
我已经通过一个简单的收集数据循环并将其推入一个数组。 然后我尝试将该数组发送到页面方法(.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' }); } }); });
只需在数据参数上附加它所期望的任何参数,如下所示:
data: "paramName=" + dataForSubmit,
或者,或者:
data: { paramName : dataForSubmit },
你应该使用这个函数: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' }); } });