2013-12-17 197 views
0

我有一個JavaScript方法來調用我所有的ajax調用,請參閱下面的內容。jquery response.d返回undefined

<script src="scripts/jquery-1.9.1.min.js" type="text/javascript"></script> 
    <script type = "text/javascript"> 
function PopulateSystemGroup_js() { 

      if ($('#<%=ddlComponentGroup.ClientID%>').val() == "0") { 
       $('#<%=ddlSystemGroup.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>'); 
       $('#<%=ddlFailureCode.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>'); 
      } 
      else { 
       $('#<%=ddlSystemGroup.ClientID %>').empty().append('<option selected="selected" value="0">Loading...</option>'); 
       $.ajax({ 
        type: "POST", 
        url: 'VB.aspx/PopulateSystemGroup', 
        data: '{ComponentGroupID: ' + $('#<%=ddlComponentGroup.ClientID%>').val() + '}', 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function(response){ 
        OnSystemGroupPopulated(response); 
        }, 
        failure: function(response) { 
         alert(response.d); 
        } 
       }); 

      } 
     } 

function OnSystemGroupPopulated(response) { 
     alert(response.d); 
      PopulateControl(response.d, $("#<%=ddlSystemGroup.ClientID %>")); 
     } 


function PopulateControl(list, control) { 

      if (list.length > 0) { 
       control.removeAttr("disabled"); 
       alert("here2"); 
       control.empty().append('<option selected="selected" value="0">Please select</option>'); 
       $.each(list, function() { 
        control.append($("<option></option>").val(this['Value']).html(this['Text'])); 
       }); 
      } 
      else { 
       control.empty().append('<option selected="selected" value="0">Not available<option>'); 
      } 
     } 

</script> 

上述被稱爲如以下HTML

<body> 
    <form id="form1" runat="server"> 
    <div> 
    ComponentGroup:<asp:DropDownList ID="ddlComponentGroup" runat="server" AppendDataBoundItems="true" 
       onchange = "PopulateSystemGroup_js();"> 
     <asp:ListItem Text = "Please select" Value = "0"></asp:ListItem>     
    </asp:DropDownList> 
    <br /><br /> 
    SystemGroup:<asp:DropDownList ID="ddlSystemGroup" runat="server"> 
     <asp:ListItem Text = "Please select" Value = "0"></asp:ListItem>     
    </asp:DropDownList> 
    <br /><br /> 
    FailureCod:<asp:DropDownList ID="ddlFailureCode" runat="server"> 
     <asp:ListItem Text = "Please select" Value = "0"></asp:ListItem>     
    </asp:DropDownList> 
    <br /> 
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick = "Submit" />     
    </div> 
    </form> 
</body> 

後面的代碼作爲

<System.Web.Services.WebMethod()> _ 
    Public Shared Function PopulateSystemGroup(ByVal ComponentGroupID As Integer) As ArrayList 
     Dim list As ArrayList = New ArrayList 
     Dim strConnString As String = ConfigurationManager.ConnectionStrings("con").ConnectionString 
     Dim strQuery As String = "select SystemGroupDescription SystemGroup,SystemGroupID from FCR_SystemGroup where ComponentGroupID [email protected] Order by SystemGroup" 


     Dim con As SqlConnection = New SqlConnection(strConnString) 
     Dim cmd As SqlCommand = New SqlCommand 
     cmd.CommandType = CommandType.Text 
     cmd.Parameters.AddWithValue("@ComponentGroupID", ComponentGroupID) 
     cmd.CommandText = strQuery 
     cmd.Connection = con 
     con.Open() 
     Dim sdr As SqlDataReader = cmd.ExecuteReader 
     While sdr.Read 
      list.Add(New ListItem(sdr("SystemGroup").ToString, sdr("SystemGroupID").ToString)) 
     End While 
     con.Close() 
     Return list 
    End Function 

上面如下功能的燒成和數據在上述數組列表加載,但是當它返回到前端時,它不會將數據填充到下拉列表中。OnSystemGroupPopulated中的alert(response.d)返回一個未定義的值。可能是什麼問題呢?

回答

0

我建議你去與客戶一個如下: 1)確保您的VB代碼返回一個JSON對象(客戶端功能的JavaScript函數提交數據之前specting之一) 2)調試你(使用Chrome例如開發人員工具) 2)嘗試解析成功(「響應」= JSON.parse(響應))json的「響應」,然後你就可以通過json對象(response.d)訪問它。

希望這會有所幫助! (對不起,我的英語)

+0

謝謝MRodriguez,讓我試試這個... – user3111059

+0

試圖VAR響應= JSON.parse(響應),但同樣的錯誤.. – user3111059

0

我建議使用Json.NET將你的響應轉換爲Json,因爲你的ArrayList在返回客戶端時沒有多大意義。 http://james.newtonking.com/json

Imports Newtonsoft.Json.Linq 

示例代碼:

Dim list As New ArrayList() 
list.Add("foo") 

Return JArray.FromObject(list) 
+0

Thansk爲建議,我下載了Newtonsoft.Json.Linq,但仍然發生同樣的問題。 – user3111059

+0

你調試過兩端了嗎? 'JArray.FromObject(list)'在服務器端不是null,'response'在客戶端真的爲空?看看整個響應對象,而不只是'response.d' –

+0

JArray.FromObject(list)is returnbing null .. – user3111059