Json Ajax参数传递&;Webmethod未启动

本文关键字:Webmethod 启动 amp Ajax 参数传递 Json | 更新日期: 2023-09-27 17:56:09

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

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

Json Ajax参数传递&;Webmethod未启动

您正在传递Id作为参数,正确的是ChurchId,就像Web方法签名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);
        }
    });

你的web方法真的被执行了吗?如果是,并且您认为它应该返回数据,那么可能是您的Web方法没有返回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")));

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