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

    How to call webservice from Ajax


    Are you looking for a way to call webservice from Ajax then read this thread to know how to call it



    Hi,

    I want to call webservice from client-side. I've used below code. I'm not getting any response and I've kept breakpoint also in my webservice it also not hitting.
    When I invoke my webservice manually its working.
    Code:
    <asp:Button ID="BtnSheet" runat="server" CssClass="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" Text="Upload" OnClientClick="doValidate();"
    OnClick="BtnSheet_Click" />

    Script:
    function doValidate() {
    var path = document.getElementById('fulSelect').value;
    if (path == '') {
    alert("Select Excel Sheet");
    return false;
    }
    else {
    LoadData(path);
    }
    }
    function LoadData(path) {
    $.ajax({
    type: "POST",
    url: "../WebService.asmx/BulkRegistration",
    contentType: "application/json; charset=utf-8",
    data: "{ fulupload: '" + path + "'}",
    datatype: "json",
    success: AjaxloadtblOutSourceSucceeded,
    failure: function() {
    alert("fail");
    }
    });

    function AjaxloadtblOutSourceSucceeded() {
    alert("sucess");
    }
    }
    </script>

    WebMethod:
    #region Bulk Registration
    [WebMethod(EnableSession = true)]
    [System.Web.Script.Services.ScriptMethod()]
    public DataTable BulkRegistration(string fulupload)
    {
    }
    #endregion

    kindly suggest.
  • #763459
    you can simple write a javascript and call web services directly from it, see below snippet
    <script language="javascript" >
    function onClick(){
    MyAjaxApplication.MyWebService1.Method1(OnComplete,
    OnTimeOut, OnError);
    return true;
    }
    function OnComplete(args) {
    document.getElementById('Label1').innerHTML = args;
    }
    function OnTimeOut(args) {
    alert("Service call timed out.");
    }

    function OnError(args) {
    alert("Error calling service method.");
    }</script>

    //in aspx define a script manager to call web service
    <asp:ScriptManager ID="ScriptManager1" runat="server" >
    <Services>
    <asp:ServiceReference Path="MyService.asmx" />
    </Services>
    </asp:ScriptManager>

    for more detail see below links
    https://msdn.microsoft.com/en-us/library/bb398995(v=vs.90).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
    http://www.codeproject.com/Articles/21045/Different-methods-to-call-Web-Services-from-AJAX

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763470
    1. Can you check the whether URL correct?
    2. Can you try to change it into static method as follows.

    public static DataTable BulkRegistration(string fulupload)
    {

    By Nathan
    Direction is important than speed


  • Sign In to post your comments