我有一個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)返回一個未定義的值。可能是什麼問題呢?
謝謝MRodriguez,讓我試試這個... – user3111059
試圖VAR響應= JSON.parse(響應),但同樣的錯誤.. – user3111059