Sunday, 23 March 2014

Calling webmethod from Jquery

Calling the web method from Client side event and Jquery;

You need to first create the webmethod,
Creating the webmethod to add the employee;

[System.Web.Services.WebMethod]
    public static string AddEmployeeDetails(int num, string  Name)
    {
        EmplyeeBAO ObjEmplyeee = new EmplyeeBAO();
        ObjEmplyeee.AddItem(num, Name);
        return "Add";

    }


Call below jqery code to insert the employee data by calling the web method which has written on code behind window;

jQuery.ajax({
            url: 'Default.aspx/AddEmployeeDetails',
            type: "POST",
            data: "{'num' : " + num + ",'Name':" + Name + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
                alert("Start!!! ");
            },
            success: function (data) {
                alert("a");
            },
            failure: function (msg) { alert("Sorry Not inserted!!! "); }
        });

No comments:

Post a Comment