[Solved] jQuery AJAX WebMethod not getting called in ASP.Net

Last Reply 5 months ago By dharmendr

Posted 5 months ago

I have one Textbox control on page with calendar extendar. I want to popup message on every date selection. For this I have used AJAX JSON method.

But my Web Method is not firing Up. Where is the problem?

HTML: 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default_test.aspx.vb" Inherits="Default_test" %>
<%@ Register Assembly="DatePicker" Namespace="DatePicker" TagPrefix="SQ" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">        
        $(document).ready(function () {
            $('#txtdoy').change(function () {
                debugger;
                var doy = $("#txtdoy").val();
                $.ajax({
                    type: "POST",
                    url: "/Default_test.aspx/GetRouteQuota",
                    data: '{doy: "' + doy + '", route: Baltal}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            });
        });
        function OnSuccess(response) {
            alert(response.d);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div><AjaxControls:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
         </AjaxControls:ToolkitScriptManager>
         <asp:TextBox ID="txtdoy" runat="server"  Font-Size="Small" Width="150px" MaxLength="11"></asp:TextBox> 
         <ajaxcontrols:calendarextender ID="CalendarExtender1" runat="server" Format="dd/MMM/yyyy" TargetControlID="txtdoy" ></ajaxcontrols:calendarextender>
    </div>
    </form>
</body>
</html>

vb code: 

<System.Web.Services.WebMethod()> _
Public Shared Function GetRouteQuota(ByVal doy As Date, ByVal route As String) As String
    Dim rstr As String = ""
        rstr = "Date of Yatra: " & doy.ToString() & ", Route: " & route
    Return rstr
End Function

 

Posted 5 months ago

Hi Sumeet,

Sumeet says:
data: '{doy: "' + doy + '", route: Baltal}',

Here you need to pass string value to route parameter.

Check this example. Now please take its reference and correct your code.

HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#txtdoy').change(function () {
                var doy = $(this).val();
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetRouteQuota",
                    data: '{doy: "' + doy + '", route: "Baltal"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.d);
                    }, error: function (response) {
                        alert(response.d);
                    }, failure: function (response) {
                        alert(response.d);
                    }
                });
            });
        });
    </script>
    <AjaxControls:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
    </AjaxControls:ToolkitScriptManager>
    <asp:TextBox ID="txtdoy" runat="server" Font-Size="Small" Width="150px" MaxLength="11"></asp:TextBox>
    <AjaxControls:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MMM/yyyy"
        TargetControlID="txtdoy">
    </AjaxControls:CalendarExtender>
</div>

C#

[System.Web.Services.WebMethod()]
public static string GetRouteQuota(DateTime doy, string route)
{
    string rstr = "";
    rstr = "Date of Yatra: " + doy.ToString() + ", Route: " + route;
    return rstr;
}

VB.Net

<System.Web.Services.WebMethod()> _
Public Shared Function GetRouteQuota(ByVal doy As Date, ByVal route As String) As String
    Dim rstr As String = ""
    rstr = "Date of Yatra: " & doy.ToString() & ", Route: " & route
    Return rstr
End Function

Screenshot