Webmethod停止工作

时间:2017-11-16 11:48:55

标签: c# jquery webmethod

我试图将值从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();&amp; countryId ='“+ countryId +”'“时,代码正常运行。

2 个答案:

答案 0 :(得分:2)

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

&

答案 1 :(得分:0)

在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']));
                    });
                }
            });
        });

希望这是有效的。