2

I have looped through a simple gathering data and pushing it into an array. I am then trying to send that array to a page method (.aspx). There is something about the array that it does not like I think. Here is my code:

//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' });
                    }
                });
            });
Matt
  • 5,542
  • 14
  • 48
  • 59

2 Answers2

4

Just append whatever parameter it's expecting on the data argument like this:

data: "paramName=" + dataForSubmit,

or, alternatively:

data: { paramName : dataForSubmit },
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • @Matt - Put this in for your error handler to see what the error is: `error:function (xhr, opt, aError){ alert(xhr.status); alert(aError); }` – Nick Craver Mar 08 '10 at 18:13
  • @Matt - That means a server error, something's blowing up on the asp.net side of things, can you hook up a debugger or logging to see what? FireBug is also tremendously helpful for this: http://www.getfirebug.com/ – Nick Craver Mar 08 '10 at 19:05
  • Firebug says my JSON is invalid? I didn't have my [Web Method] static so that explains the server-side problem. Sorry, man I'm new to this stuff.. – Matt Mar 08 '10 at 23:05
  • @Matt - You fix the server-side, still an issue? If so update and we'll see what the problem is. – Nick Craver Mar 08 '10 at 23:13
0

You shoud use this Function: JSON.stringfy,so:

//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' });
                    }
                });
Mike
  • 11
  • 2