jquery response.d返回undefined

时间:2013-12-17 12:00:38

标签: asp.net vb.net

我有一个JavaScript方法来调用我的所有ajax调用,请参阅下面的内容。

 <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 =@ComponentGroupID 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

以上是函数正在触发并且数据被加载到上面的arraylist中,但是当它返回到前端时,它没有将数据填充到下拉列表中.OnSystemGroupPopulated中的警报(response.d)返回未定义的。可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我建议您跟随客户一个: 1)确保你的vb代码返回一个JSon对象(你的客户端函数正在编写一个) 2)在提交数据之前调试javascript函数(例如使用Chrome Developer工具) 2)尝试在成功时解析json的“响应”(var response = JSON.parse(response)),然后你就可以访问它了一个json对象(response.d)。

希望这有帮助! (对不起我的英文)

答案 1 :(得分: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)