我正在尝试访问在 javascript 数组中返回的 2 个搜索结果。
如果在此处检查如何执行此操作:访问/处理(嵌套)对象、数组或 JSON ,然后我根据此结果集进行了尝试:
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"facet":"false",
"fl":"id,title,friendlyurl,avatar,locpath,objectid,objecttype",
"indent":"off",
"q":"title_search:*castle*",
"wt":"json",
"defType":"lucene"
}
},
"response":{
"numFound":2,
"start":0,
"docs":[
{
"title":"castle a",
"objecttype":1,
"friendlyurl":"castle-a",
"avatar":"6_887_castle-a.JPG",
"objectid":6
},
{
"title":"castle b",
"objecttype":1,
"friendlyurl":"castle-b",
"avatar":"794_360_13j-castle-by-night.jpg",
"objectid":794
}
]
}
}
for (var i = 0, l = data.response.numFound; i < l; i++) {
console.log(data.response.docs[i].title);
console.log(data.response.docs[i].objecttype);
console.log(data.response.docs[i].friendlyurl);
}
如果我直接调用该服务,我确实会返回结果,但我的 Chrome 控制台日志中仍然会收到一个 data=null 。为什么会这样?
这是相关代码
$("#searchfavs").change(function () {
$.ajax({
type: "GET",
url: "/weddingservice/searchfavoritecompany/?q=" + $("#searchfavs").val(),
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
data = $.parseJSON(data);
console.log(data);
console.log(data.response);
console.log(data.response.numFound);
for (var i = 0, l = data.response.numFound; i < l; i++) {
console.log(data.response.docs[i].title);
console.log(data.response.docs[i].objecttype);
console.log(data.response.docs[i].friendlyurl);
}
}
});
$(this).parent().hide('slow');
});
如何循环查看我的结果?