Monday, September 17, 2018

jQuery Ajax POST using WebMethod in c#

Code Snippet:

UI:

<asp:Button ID="AjaxCall" runat="server" Text="Call Ajax" OnClientClick="AjaxCall1();" />


jQuery Ajax:

        function AjaxCall1() {
            debugger;
            $.ajax({
                type: "POST",
                url: "AjaxTest.aspx/TestAjaxCall",
                data: '{postParam: "Hello Ajax web method" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function (response) {
                    alert("Success");
                },
                failure: function (response) {
                    alert("failure");

                }
            });
        }

c# WebMethod:

        [WebMethod]
        public static string TestAjaxCall(string postParam)
        {
            return postParam;       
        }




Sample snippet that explains jQuery Ajax POST using WebMethod in c#


No comments:

Post a Comment