views:

102

answers:

1

I am using jQuery ajax to pass a string array to a page method. I need to know what to do with that data once it gets to that page method. My goal is to insert it into a DB but before that I need to make sense of the array. As it is, it is coming over like this: { data : theArray} which correlates to {data: 1,2,3,4,5,6}. But 4,5, and 6 represent another row in the underlying DB and so would 7,8 and 9, and so on and so forth. I am new to Ajax.

Here is the jQuery:

//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());
                });
                //send array to method
                callScriptMethod('DailyReceipts.aspx/saveData', { theData: dataForSubmit });
            });

            function callScriptMethod(url, jsonObject, callback, async) {

                callback = callback || function() { };
                async = (async == null || async);

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: url,
                    data: JSON.stringify(jsonObject),
                    dataType: "json",
                    async: async,
                    success: function(jsonResult) {
                        if ('d' in jsonResult)
                            callback(jsonResult.d);  
                        else
                            callback(jsonResult);
                    },
                    error: function() {
                        alert("Error calling '" + url + "' " + JSON.stringify(jsonObject));
                        callback([]);
                    }
                });
            }

And here is the page method, made static and as a [WebMethod]:

   [WebMethod]
        public static void saveData(string[] theData)
        {

            //iterate the array
            for (int i = 0; i < theData.Length; i++)
            {

            }

        }
+1  A: 

Here is an article on calling .aspx page method with jQuery.

What you can do is use the information in that article to post a JSON-formatted string to the method using a parameter, then use JSON.NET to parse the string into an object.

Dave Swersky

related questions