Json Ajax参数传递& Web方法没有解雇

时间:2014-04-02 18:01:16

标签: c# jquery asp.net ajax json

在我的Ajax函数中,我试图将一个int参数传递给Web方法,但它没有成功。 在这里,我粘贴我的代码

Ajax功能

    $('#drpChurchNames').on('change', function () {
        //alert($(this).val());
        LoadFathersToChurch(churchId)
    });

    function LoadFathersToChurch(churchId) {

        var url = '<%=ResolveUrl("WebMethods.aspx/GetFatherNames") %>';
        $.ajax({
            url: url,
            type: "GET",
            dataType: "json",
            data:'{ Id: " '+churchId +' "}',
            contentType: "application/json; charset=utf-8",
            success: function (Result) {
                $.each(Result.d, function (key, value) {
                    $("#drprevfather").append($("<option></option>").val
                    (value.Id).html(value.FatherName));
                });
            },
            error: function (e, x) {
                alert(x.ResponseText);
            }
        });

    }

这是我的WebMethod

      [WebMethod]
 [ScriptMethod(UseHttpGet = true)]
 public static List<FatherNames> GetFatherNames(int ChurchId)
 {
     List<FatherNames> FathersList = new List<FatherNames>();
     try
     {
         SqlCommand comChurchNames = new SqlCommand("GetFathers", conDB);
         comChurchNames.CommandType = CommandType.StoredProcedure;
         comChurchNames.Parameters.Add("@Id", SqlDbType.Int);
        comChurchNames.Parameters["@Id"].Value = ChurchId;
        if (conDB.State == ConnectionState.Closed)
            conDB.Open();

        SqlDataReader rdr = comChurchNames.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(rdr);
        foreach (DataRow r in dt.Rows)
        {
            FathersList.Add(new FatherNames
            {
                Id = (int)r["Id"],
                FatherName = r["FatherName"].ToString()

            });
        }
    }

这是我的SP

ALTER PROCEDURE [dbo].[GetFathers]
@SelectIndexName int
AS
BEGIN
Select * from dbo.RevFathers
Where ChurchId = @SelectIndexName
END

2 个答案:

答案 0 :(得分:2)

您正在传递Id作为参数,正确的是ChurchId,就像webmethod签名GetFatherNames(int ChurchId)一样。

有正确的方法:

$.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        data:'{ ChurchId: " '+churchId +' "}',
        contentType: "application/json; charset=utf-8",
        success: function (Result) {
            $.each(Result.d, function (key, value) {
                $("#drprevfather").append($("<option></option>").val
                (value.Id).html(value.FatherName));
            });
        },
        error: function (e, x) {
            alert(x.ResponseText);
        }
    });

答案 1 :(得分:0)

你的网络方法真的被执行了吗?如果是,并且您认为它应该返回数据,那么可能是您的webmethod没有返回JSON,因此jQuery可能会生成错误而不会触发成功。

我在global.asax中有这个来配置XML和JSON的输出:

using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web.Http;

放在app_start

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));

我知道这不是一个直接的答案,但我还不能发表评论所以我会先问上面的问题是关于webmthod被调用。