解析通过$ .getJSON接收的json数据时出现问题

时间:2011-06-24 05:16:30

标签: jquery json javascript-events

当我使用JSON数据时:

$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+elegant+"&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(newresult){

并将此JSON数据用作

$.each(newresult.highlighting, function(i, hitem){
        alert(hitem.description[0]);

});

然后我没有得到描述字段值。

但是,当我在变量中分配整个数据时,比如newresult,然后运行$.each()它就像我在http://jsfiddle.net/taL8x/1/中所做的那样工作。请告诉我通过getJSON()使用数据时出现的问题。

2 个答案:

答案 0 :(得分:0)

您的请求可能已被同源政策停止。如果该服务位于其他网址上,则必须使用JSONP

答案 1 :(得分:0)

如果在alert处传递行,则newresult.highlighting应该是对象或数组。那么,这不是同源策略的问题。您正在为回调指定json.wrf=?。 可能你错过了突出显示数组中元素的结构。也许。 尝试访问directry,并确认数组的结构。 你期待的是:

{
"highlighting": [ { "description" : "foo" }, { "description" : "bar" } ]
}

但可能是

{
"highlighting": { "A001" : { "description" : "foo" }, "A002" : { "description" : "bar" } }
}