title: How can I call a WebMethod to return json with ajax?
body: <p>I get 404 every time I try this. I can't find the error in my code. I have other webmethod to delete and it works. I am using a WebForm , ADO.NET with a connection string, .NET 4.5.</p>
  
  <pre><code>[System.Web.Services.WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public static string ListarTiposLicencia()
  {
      TipoLicenciaBL objTipoLicencia = new TipoLicenciaBL();
      //Return a DataTable from database with a stored procedure
      DataTable dt = objTipoLicencia.DevolverListaTipoLicencia(String.Empty, String.Empty);
      return DataTableToJSONWithJavaScriptSerializer(dt);
  }
  
  public static string DataTableToJSONWithJavaScriptSerializer(DataTable table)
  {
      JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
      List&lt;Dictionary&lt;string, object&gt;&gt; parentRow = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      Dictionary&lt;string, object&gt; childRow;
      foreach (DataRow row in table.Rows)
      {
          childRow = new Dictionary&lt;string, object&gt;();
          foreach (DataColumn col in table.Columns)
          {
              childRow.Add(col.ColumnName, row[col]);
          }
          parentRow.Add(childRow);
      }
      return jsSerializer.Serialize(parentRow);
  }
  </code></pre>
  
  <p>This is the ajax call:</p>
  
  <pre><code>$(document).ready(function () {
      $("#obtenerLicencias").click(function () {
          $.ajax({
              type: "POST",
              url: "CnfListaTipoLicencias.aspx/ListarTiposLicencia",
              data: '{ }',
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              async: true,
              cache: false,
              success: function (data) {
                  alert(JSON.parse(data));
              },
              failure: function (response) {
                  alert("Error");
              },
              error: function (error) {
                  alert("error");
              }
          });
  
      });
  });
  </code></pre>
  
  <p>Edit:
  I have tried this, but it doesnot work, I get 404 again:</p>
  
  <pre><code>[System.Web.Services.WebMethod]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public static string ListarTiposLicencia()
      {
          TipoLicenciaBL objTipoLicencia = new TipoLicenciaBL();
          DataTable dt = objTipoLicencia.DevolverListaTipoLicencia(String.Empty, String.Empty);
          string json = JsonConvert.SerializeObject(dt, Formatting.Indented);
          return json;
      }
  </code></pre>