我有一些從asp.net函數返回的json。我只測試了這一點,到目前爲止,但我目前所面對的是:通過json數組迭代從asp.net函數返回
Public Structure myarray
Dim name As String
End Structure
<WebMethod()> _
Public Shared Function temp(ByVal strTerm As String) As String
Dim user(1) As myarray
user(0).name = "John"
user(1).name = "Joe"
Dim serializer As New JavaScriptSerializer()
Dim arrayJson As String = serializer.Serialize(user)
Return arrayJson
End Function
和jQuery的我:
jQuery.ajax({
type: "POST",
url: "default.aspx/temp",
data: "{'strTerm':'" + req.term + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var suggestions = [];
$.each(data, function (i, val) {
//alert(val);
suggestions.push(val);
});
add(suggestions);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('unable to create ticket');
}
});
什麼,我在
尋找$.each(data, function (i, val) {
//alert(val);
suggestions.push(val);
});
是要獲得我指定的每個名稱,即約翰& Joe並將它們添加到「suggestions」數組。不確定Json是否使用正確的格式。
返回的JSON是:
[{"name":"John"},{"name":"Joe"}]
任何想法如何是做到這一點的正確方法是什麼?
謝謝,
(與上面相同)...是的,我試過了,但我似乎得到的是「未定義」,如果我要警惕(val.name);出。我想知道如果它的JSON結構... – thegunner 2011-12-19 01:20:11
您是否嘗試過data.dd而不是數據?就像我說的,很可能ASP.NET已經將響應包裝在一個d屬性中。 – villecoder 2011-12-19 01:45:01
是啊試過data.d ...也不太對。基本上我在.net上試用這篇文章:http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/ – thegunner 2011-12-19 01:56:05