2013-12-17 151 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