Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Calling a C# method using jQuery on the client side

1.47/5 (14 votes)
2 Oct 2011CPOL 31.4K  
Create a web service name WebMethod.SVC in the CS file and add the method: [OperationContract] [WebInvoke(Method = POST)] public void DeleteRec(string ID) { DelUser(Convert.ToInt32(ID)); }On client side, give the path and call the...
Create a web service name WebMethod.SVC in the CS file and add the method:

C#
[OperationContract]
        [WebInvoke(Method = "POST")]
        public void DeleteRec(string ID)
        {
            DelUser(Convert.ToInt32(ID));
        }


On client side, give the path and call the method:

C#
function DeleteMessage() {            
            var ID = $('[id$="hdnUserId"]').val();
            var url = '<%=ResolveUrl("~/WebMethods.svc/DeleteRec")%&  gt;';            
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: '{"ID":"' + ID + '"}',
                dataType: "json",
                success: function (response) {
                    document.location.href = '<%= ResolveUrl("~/Test1.aspx")%>';
                },
                error: function (xhr, ajaxOptions, thrownError) {
                }
            });
        }


This was the other way which I got.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)