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' });
                    }
                });
            });
2
  • Do you get some error? How does your server side script look like? It seems that you are using PageMethods. Commented Mar 8, 2010 at 17:30
  • It reaches my error handling. Server-side looks like this: public void saveData(string[] theData) Commented Mar 8, 2010 at 17:54

2 Answers 2

4

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

data: "paramName=" + dataForSubmit,

or, alternatively:

data: { paramName : dataForSubmit },
Sign up to request clarification or add additional context in comments.

4 Comments

@Matt - Put this in for your error handler to see what the error is: error:function (xhr, opt, aError){ alert(xhr.status); alert(aError); }
@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: getfirebug.com
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 - You fix the server-side, still an issue? If so update and we'll see what the problem is.
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' });
                    }
                });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.