tags:

views:

240

answers:

2

Hi, I'm trying to save data from HTML table to database using jquery to call a page method. However, jquery call fails with : 500 internal server error.. Any advice?

Below is the code:

SCRIPT:

    $(document).ready(function() {


        $("#save").click(function(event) {

            var i = 0;
            var inputs = new Array();
            $("#tbNames").find("input").each(function() {         
                inputs[i] = [this.id, this.value];
                i++;
            });

            var columnsCount = 2;
            $.ajax({

                type: "POST",
                url: "Default2.aspx/Save",
                contentType: "application/json; charset=utf-8",
                data: "{inputs: inputs, columnsCount: columnsCount}",
                dataType: "json",
                success: AjaxSucceeded,
                 error: AjaxFailed
            });
        });
        function AjaxSucceeded(result) {
            alert(result.d);
        }

        function AjaxFailed(result) {
            alert(result.status + ' ' + result.statusText);
        }

    });



    </script>

HTML:

Name Task Delete

    <input id="save" type="button" value="Save data" />

and C# code of page method:

 [WebMethod ()]
    public static List<Hashtable> ParseJson(string[] array, int rowsCount)
    {
    var result = new List<Hashtable>();

    List<string[]> list = new List<string[]>();
    foreach (var item in array)
    {
        int comma = item.IndexOf(',');
        list.Add(new string[] { item.Substring(0, comma), item.Substring(comma + 1) });
    }

    int i = 0;
    while (i < list.Count)
    {
        var dict = new Hashtable();
        int j = i + rowsCount;
        for (; i < j; i++)
        {
            dict.Add(list[i][0], list[i][1]);
        }

        result.Add(dict);
    }

    return result;

}

[WebMethod()] 
public static string Save(string[] inputs, int columnsCount)
{
    List<Hashtable> r = ParseJson(inputs, columnsCount);

    List<TestClass> listOfTasks = new List<TestClass>();
    foreach (var item in r)
    {
        TestClass tc = new TestClass();
        tc.name  = (string)item["name"];
        tc.task = (string)item["task"];

        listOfTasks.Add(tc);
    }

    DBManager dbm = new DBManager();
    string actionResult = dbm.SaveData(listOfTasks);
    if (actionResult == String.Empty)
    {
        return "Saved!";
    }
    else
    {
        return actionResult;
    }
}
+1  A: 

500 is a generic error. There is something wrong with the webservice that your JQuery is using. You need to look at the logs written by your service to find out what was wrong, or you should set a breakpoint inside your service code to debug it.

Dave Markle
The breakpoint of C# Save() function was not hit. So, it seems that a call to page method is a failure.On the same page I put a 'hello world' button which calls also page method and it works fine.The error occurs in the : $.ajax({ type: "POST", url: "Default2.aspx/Save", contentType: "application/json; charset=utf-8", data: "{inputs: inputs, columnsCount: columnsCount}", dataType: "json", success: AjaxSucceeded, error: AjaxFailed });
Neno
+1  A: 

So, I'm going to answer my own question:

The link , where all is well explained is :

http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/

The key is json2.js and its stringify function. After implementing of that function AJAX call looks like this:

$.ajax({ type: "POST", url: "Default2.aspx/Save", contentType: "application/json; charset=utf-8", data: "{'inputs':" + JSON.stringify(inputs) + ",'columnsCount':1 }", dataType: "json", success: AjaxSucceeded, error: AjaxFailed });

Neno