如何通过ajax调用WebMethod返回json?

时间:2016-09-07 05:03:56

标签: c# jquery asp.net json ajax

每次尝试这个时我都会得到404。我在代码中找不到错误。我有其他webmethod删除,它的工作原理。我正在使用WebForm,ADO.NET和连接字符串,.NET 4.5。

[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电话:

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

    });
});

编辑: 我试过这个,但它不起作用,我再次得到404:

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

2 个答案:

答案 0 :(得分:0)

  1. 在程序包管理器控制台中运行以下命令以安装Newtonsoft:

    Install-Package Newtonsoft.Json

  2. 请输入以下代码 var a = Newtonsoft.Json.JsonConvert.DeserializeObject(parentRow);

答案 1 :(得分: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)填充了一些数据。