【问题标题】:Webmethod Stops Working网络方法停止工作
【发布时间】:2018-04-29 22:43:23
【问题描述】:

我正在尝试将值从 jquery 传递到 webmethod。但是当 1 次传递值 webmethod 不启动时。当我删除传递值代码时,它开始正常工作有什么问题? JS代码。

$(function () {
    $.ajax({
        type: "POST",
        url: "ContryCityDDL.aspx/GetCountry",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            var ddlcontry = $("[id*=ddlcontry]");
            ddlcontry.empty().append('<option selected="selected" value="0">Please select</option>');
            $.each(r.d, function () {
                ddlcontry.append($("<option></option>").val(this['Value']).html(this['Text']));
            });
        }
    });

    $("#ddlcontry").change(function () {
        alert($('option:selected', $(this)).val());
       var countyId = $(this).val();

        $("#HiddenField1").val($('option:selected', $(this)).val());


        $.ajax({
            type: "POST",
            url: "ContryCityDDL.aspx/GetCity&countryId='"+countryId+"'",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var ddlcity = $("[id*=ddlcity]");
                ddlcity.empty().append('<option selected="selected" value="0">Please select</option>');
                $.each(r.d, function () {
                    ddlcity.append($("<option></option>").val(this['Value']).html(this['Text']));
                });
            }
        });
    });
});

C#

[WebMethod]
public static List<ListItem> GetCity(string countryId)
{

    string constrng;
            string query = "Select CityId,CityName From City"; 
    //string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

     constrng = "Data Source=(LocalDB)\\v11.0;AttachDbFilename='C:\\Users\\Abdul Basit Mehmood\\Desktop\\DropdownAJAX\\DropdownAJAX\\App_Data\\ContryCity.mdf';Integrated Security=True";

    using (SqlConnection con = new SqlConnection(constrng))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            List<ListItem> customers = new List<ListItem>();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(new ListItem
                    {
                        Value = sdr["CityId"].ToString(),
                        Text = sdr["CityName"].ToString()
                    });
                }
            }
            con.Close();
            return customers;
        }
    }
}

当我从 c# 代码中删除 'string countryId' 并从 js 中删除 `var CountyId = $(this).val();&countryId='"+countryId+"'" 时,代码运行正常。

【问题讨论】:

  • 请有人告诉我为什么 GetCity(string countryId) 不起作用。当我想读取函数中的一些参数时,问题是什么。在js中改变没有任何区别

标签: c# jquery webmethod


【解决方案1】:

您希望将 ContryId 作为 ajax 调用中的查询字符串传递,在这种情况下使用 ? 字符而不是 &amp; 之类的

"ContryCityDDL.aspx/GetCity?countryId='"

【讨论】:

  • 编译器没有显示任何错误,但问题是 GetCity() 函数运行正常,但是当我将其更改为 GetCity(string countryId) 时,它没有运行。我需要 countryId 根据所选国家/地区填充城市下拉列表。
  • @AbdulBasitMehmood,编译器不会对客户端代码显示错误...表示JS代码
  • 为什么 GetCity() 工作时 GetCity(string countryId) 不工作?
  • 请有人告诉我为什么 GetCity(string countryId) 不起作用。当我想读取函数中的一些参数时,问题是什么。在js中改变没有任何区别。
【解决方案2】:

在 ddlcontry 更改功能上试试这个,

 $("#ddlcontry").change(function () {
            alert($('option:selected', $(this)).val());
            var countryId = $(this).val();

            $("#HiddenField1").val($('option:selected', $(this)).val());


            $.ajax({
                type: "POST",
                url: "ContryCityDDL.aspx/GetCity",
                data: "{'countryId':'" + countryId + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    var ddlcity = $("[id*=ddlcity]");
                    ddlcity.empty().append('<option selected="selected" value="0">Please select</option>');
                    $.each(r.d, function () {
                        ddlcity.append($("<option></option>").val(this['Value']).html(this['Text']));
                    });
                }
            });
        });

希望这行得通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多