我有一个由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下拉列表时,我收到一个错误,显示为Uncatch TypeError:无法使用'in'运算符在[中搜索'length'
问题出在Web方法中,还是没有正确处理结果?
1条答案
按热度按时间erhoui1w1#
日志(result.d)返回的数组是否与描述中提到的一样?