3

I am calling webmethod to populate select tag. success return but does not populate select options

here is my call

function getCities(country2) {
        $.ajax({
            type: 'POST',
            url: 'getCities.aspx/getCitiesArray',
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            data: "{countryName:'" + (country2) + "'}",
            success: function (msg) {
                alert(msg);
                $("#city").empty().append($("<option></option>").val("[-]").html("select city"));
                $("#city").append($("<option></option>").val("Other").html("Not in the List"));
                var htm = "";
                $.each(msg.d, function () {
                    $("#city").append($("<option></option>").val(this['Value']).html(this['Text']));
                });
            },
            error: function () {
                alert("Ajax Error");
            }
        });


and the webmethod in Visual studio 2005. ASP.NET 2.0

[WebMethod]
        public static ArrayList getCitiesArray(string countryName)
        {
            ArrayList emptyArrayList = new ArrayList();
            string sql = "select ISNULL(CityName,'-') as CityName, ISNULL(CityCode,1) as CityCode from ListCities where CountryID = (select ISNULL(CountryID,0) from ListCountries where CountryName = '" + countryName + "')";
            DataTable dtCities = new DataTable();
            dtCities = DBUtils.GetDataTable(sql);
            ArrayList lstArrCities = new ArrayList();
            if ((dtCities != null) && (dtCities.Rows.Count > 0))
            {                
                for (int i = 0; i < dtCities.Rows.Count; i++)
                {
                    lstArrCities.Add(new ListItem(dtCities.Rows[i]["CityName"].ToString(), dtCities.Rows[i]["CityCode"].ToString()));                    
                }                                
                return lstArrCities;
            }
            return emptyArrayList;
        }
Raghuveer
  • 2,630
  • 3
  • 29
  • 59
  • 4
    You're STILL using Visual studio 2005 with ASP.NET 2.0? Really?????? – frenchie Jun 10 '14 at 10:08
  • Take a look at this question and the answer I wrote http://stackoverflow.com/questions/18244696/how-to-return-json-with-asp-net-jquery as well as the question linked in the comments. – frenchie Jun 10 '14 at 10:10
  • 1
    What is the value of msg when it comes back from the AJAX call? – sakir Jun 11 '14 at 11:00

1 Answers1

0

I'd suggest you spend some more time on debugging your code to find the source of the problem. NArrow it down the cause of the problem to client or server first. For example:

  1. What is the value of msg when it comes back from the AJAX call? If it's NULL or something unexpected stop looking at the client side and dig into the server side code before re-attacking the front end.
  2. What does your SELECT SQL return when you run it external to the application? If it's not returning any data you need to go even deeper into your database and work on the data side.

The answer to your problem is going to be very difficult to determine based on the code you've pasted - especially without your data.

Break it down into it's constituent parts and only then carry on building. Good luck.

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46