如何访问此数组或对象并绑定到dropdownlist



我有一个由web方法生成的字符串/对象

[
{
"id": 367,
"location": "Gagra"
},
{
"id": 368,
"location": "Gudauta"
},
{
"id": 369,
"location": "Sukhumi"
},
{
"id": 370,
"location": "Gulripshi"
},
{
"id": 371,
"location": "Ochamchira"
},
{
"id": 372,
"location": "Tkvarcheli"
},
{
"id": 373,
"location": "Gali"
}
]

Web方法是

<WebMethod()>
Public Function PopulateStates(ByVal idLocation As Integer) As String
Dim Locations As List(Of LocationData) = New List(Of LocationData)
Dim JSONString As String
Dim adp As New StatesTableAdapter
Dim ds As StatesDataTable = adp.GetStatesByCountry(idLocation)
If ds.Rows.Count > 0 Then
For Each row As StatesRow In ds
Dim ld As LocationData = New LocationData With {
.id = row.idState,
.location = row.locName
}
Locations.Add(ld)
Next
End If
JSONString = JsonConvert.SerializeObject(Locations, Formatting.Indented)
Return JSONString
End Function
Public Class LocationData
Public Property id As Integer
Public Property location As String
End Class

我的ajax调用看起来像

$('#ddlCountry').change(function (e) {
$.ajax({
type: "POST",
url: "/TestWebService.asmx/PopulateStates",
contentType: "application/json; charset=utf-8",
data: '{"idLocation":"' + $(this).val() + '"}',
dataType: "json",
success: function (result, status, xhr) {
console.log(result.d);
$.each(result.d, function (key, value) {
$("#ddlState").append($("<option></option>").val(value.id).html(value.location));
});
},
error: function (xhr, status, error) {
alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}
});
});

});

当字符串试图绑定到State Dropdownlist时,我会收到一个错误未捕获的类型错误:无法使用"in"运算符在[中搜索"length">

是我的Web方法有问题,还是我没有正确处理结果?

is console.log(result.d(是否返回描述中提到的数组?

最新更新