Call WebMethod from other ASPX page using jQuery AJAX

Last Reply 8 days ago By dharmendr

Posted 8 days ago

if webmethod is also on another aspx page.

want to call webmethod public function from another aspx page

You are viewing reply posted by: dharmendr 8 days ago.
Posted 8 days ago

Hi mukesh1,

Yes it is possible. For this you need to specify correct URL. Check the below example.

In the below example i have a WebMethod GetCurrentTime which is in Default page and i am calling the WebMethod in CS page.

HTML

CS.aspx

<div>
    Your Name :
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
    <input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ShowCurrentTime() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetCurrentTime", // Specify the WebMethod page url
            data: '{name: "' + $("#txtUserName").val() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) { alert(response.d); },
            error: function (response) { alert(responseresponseText); },
            failure: function (response) { alert(response.d); }
        });
    }
</script>

Code

Default.aspx.cs

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return "Hello " + name + Environment.NewLine + "The Current Time is: " + DateTime.Now.ToString();
}

Screenshot

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html