2016-09-07 14 views
0

私はこれを試すたびに404を取得します。私のコードでエラーを見つけることができません。私は削除する他のwebmethodを持っており、それは動作します。私はWebForm、ADO.NETと接続文字列、.NET 4.5を使用しています。WebMethodをajaxでjsonを返すように呼び出すにはどうすればよいですか?

[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<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>(); 
    Dictionary<string, object> childRow; 
    foreach (DataRow row in table.Rows) 
    { 
     childRow = new Dictionary<string, object>(); 
     foreach (DataColumn col in table.Columns) 
     { 
      childRow.Add(col.ColumnName, row[col]); 
     } 
     parentRow.Add(childRow); 
    } 
    return jsSerializer.Serialize(parentRow); 
} 

このAJAX呼び出しです:私は再び404を取得し、私はこれを試してみましたが、それは仕事をdoesnot :

$(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"); 
      } 
     }); 

    }); 
}); 

編集

[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; 
    } 
+0

aspxページを作成してエラーがないことを確認してください。 – Pradeep

+0

データプロパティを削除し、 –

+0

を参照してください。プロジェクトをビルドするときにコンパイルエラーは発生しません。 DataTableをJsonに変換する方法は、DataTableを必要とし、文字列を返します。 – silver1991

答えて

0

この

DataTable dt = objTipoLicencia.DevolverListaTipoLicencia(String.Empty, String.Empty); 
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
      List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 
      Dictionary<string, object> row; 
      foreach (DataRow dr in dt.Rows) 
      { 
       row = new Dictionary<string, object>(); 
       foreach (DataColumn col in dt.Columns) 
       { 
        row.Add(col.ColumnName, dr[col]); 
       } 
       rows.Add(row); 
      } 
      return serializer.Serialize(rows); 

を試してみてくださいしかし、DataTableの(dt)は、いくつかのデータを取り込むこと、それは確認してください。

+0

私はデータとcontenTypeをajax呼び出しとこのコードに追加しました。それは動作します、ありがとう – silver1991

0
  1. ランインクルードPackage ManagerコンソールのNewtonsoftをインストールするコマンドを実行します。

    インストール・パッケージNewtonsoft.Json

  2. 次のコード するvar A = Newtonsoft.Json.JsonConvert.DeserializeObject(parentRow)を置きます。

+0

私の編集を見てください、それは正しいですか? – silver1991

関連する問題