2014-06-10 2 views
3

webmethod를 호출하여 select 태그를 채 웁니다. 성공 반환하지만 옵션을 선택 여기 jQuery Ajax 웹 메소드 호출?

을 채우지 않는 것은 내 전화

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"); 
      } 
     }); 


및 Visual Studio에서의 WebMethod입니다 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; 
     } 
+4

당신은 여전히 ​​Visual Studio 2005 with ASP.NET 2.0을 사용하고 있습니까? 정말?????? – frenchie

+0

이 질문과 내가 http://stackoverflow.com/questions/18244696/how-to-return-json-with-asp-net-jquery와 의견에 링크 된 질문을 썼는지보십시오. – frenchie

+1

AJAX 호출에서 돌아올 때 msg의 값은 무엇입니까? – sakir

답변

0

나 ' d는 문제의 원인을 찾기 위해 코드를 디버깅하는 데 더 많은 시간을 할애 할 것을 제안합니다. 먼저 클라이언트 또는 서버에 문제의 원인을 알려줍니다. 예 :

  1. AJAX 호출에서 돌아올 때 msg의 값은 무엇입니까? NULL이거나 예상치 못한 경우 프런트 엔드를 다시 공격하기 전에 클라이언트 측을보고 서버 측 코드를 조사하십시오.
  2. SELECT를 응용 프로그램 외부에서 실행할 때 반환 할 값은 무엇입니까? 데이터를 반환하지 않으면 데이터베이스에 더 깊이 들어가 데이터 측면에서 작업해야합니다.

붙여 넣은 코드를 기반으로 문제를 해결하는 것은 매우 어려울 것입니다 - 특히 데이터가없는 경우.

구성 요소로 분해 한 다음 건물을 계속 옮깁니다. 행운을 빕니다.