title: How to call WebMethod?
body: <p>I am trying to call a <code>WebMethod</code> from JavaScript. So far I have:</p>
  
  <p>The EMSWebService.asmx:</p>
  
  <pre><code>namespace EMSApplication.Web.WebServices
  {
      /// &lt;summary&gt;
      /// Holds the Webservice methods of EMSApplication
      &lt;/summary&gt;
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [System.ComponentModel.ToolboxItem(false)]     
      [System.Web.Script.Services.ScriptService]
      public class EMSWebService : System.Web.Services.WebService
      {
          [WebMethod]
          public string HelloWorld()
          {
              return "Hello World";
          }
      }
  }
  </code></pre>
  
  <p>In the aspx page I have added the followings:</p>
  
  <pre><code>&lt;asp:ScriptManager ID="ScriptManager" runat="server"&gt;
      &lt;Services&gt;
          &lt;asp:ServiceReference Path="~/WebServices/EMSWebService.asmx" /&gt;
      &lt;/Services&gt;
  &lt;/asp:ScriptManager&gt;
  &lt;input onclick="callWebMethod();" id="btn" type="button" value="Click Me" /&gt;
  </code></pre>
  
  <p>And the JavaScript is:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function callWebMethod() {
          EMSApplication.Web.WebServices.EMSWebService.HelloWorld(OnComplete, OnError);            
      }
  
      function OnComplete(result) {
          alert(result);
      }
  
      function OnError(result) {
          alert(result.get_message());
      }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>But the method is not executing. I am getting following JavaScript error:</p>
  
  <blockquote>
    <p>EMSApplication not defined.</p>
  </blockquote>
  
  <p>Is there anything I am missing? Do I need to do some other configuration?</p>
  
  <p>The Project structure is depicted below:</p>
  
  <p><img src="https://i.stack.imgur.com/ZmAPy.png" alt="enter image description here"></p>
  
  <p>JavaScript and components is in Login.aspx.</p>
  
  <p>Is there any significance of the URL of <code>[WebService(Namespace = "http://tempuri.org/")]
  </code>   </p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>I have also tried this by using jQuery and modify the aspx page as:</p>
  
  <pre><code>$(document).ready(function () {
          $("#btn").click(function () {                
              $.ajax({
                  type: "POST",
                  url: "../WebServices/EMSWebService.asmx/HelloWorld",
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (response) {
                      alert(response.d);                        
                  },
                  failure: function (msg) {                        
                      alert(msg.d);
                  }
              });
              return true;
          });
      });
  </code></pre>
  
  <p>I have written <code>System.Diagnostics.Debug.WriteLine("Hello World");</code> inside the <code>WebMethod</code>, it is executing that i.e., it is printing "Hello World" in the Output Window of the Visual Studio but I am not getting any alert message.</p>