You must Sign In to post a response.
  • Category: JQuery

    Calling c# method into jquery ajax

    Hi,
    I am calling a c# method using jquery ajax
    My c# method is in .aspx.cs page
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]

    public List<ENITYT_SUPPLY.clsDemandDetails> DemandDetails()
    {
    /////////////////
    }


    my ajax script in aspx page is

    $.ajax
    ({

    type: "POST",

    url: "TMSGeoLocator.aspx/DemandDetails",

    dataType: "xml",
    async: true,
    success: function (data) {
    demandMap = $(data);
    },
    error: function (xhr, status, err) {
    var err = eval("(" + xhr.responseText + ")");
    alert(err.Message);
    },
    async: false
    });

    The same c# method if i call using webservices(.asmx) its firing the method. if i use in .aspx.cs page its not working can anyone tell me the reason.
  • #762252
    Hello Naresh,

    Refer the below code :

    <script type="text/C#" runat="server">
    [WebMethod]
    public static string GetDate()
    {
    return DateTime.Now.ToString();
    }
    </script>

    $.ajax({
    type: 'POST',
    url: 'PageName.aspx/GetDate',
    data: '{ }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
    // Do something interesting here.
    }
    });


    You can go through this link for reference :

    http://www.c-sharpcorner.com/UploadFile/dacca2/understand-jquery-ajax-function-call-code-behind-C-Sharp-method/


    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments