ajax – 使用jQuery解析JSON响应

前端之家收集整理的这篇文章主要介绍了ajax – 使用jQuery解析JSON响应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的一个应用程序中处理 JSON响应.我已成功使用jsonp建立连接.但是我无法解析我的回复.

码:@H_403_2@

<script type='text/javascript'>
(function($) {
var url = 'http://cooktv.sndimg.com/webcook/sandBox/perf/topics.json';

$.ajax({
   type: 'GET',url: url,async: false,jsonpCallback: 'callback',contentType: "application/json",dataType: 'jsonp',success: function(json) {
       console.log(json.topics);
       $("#nav").html('<a href="">'+json.topics+"</a>");
    },error: function(e) {
       console.log(e.message);
    }
});

})(jQuery);
</script>

输出我得到:@H_403_2@

[object Object],[object Object],[object Object]

响应示例:@H_403_2@

callback({"topics":[{"name":"topic","content":[{"link_text":"link","link_src":"http://www.foodnetwork.com/"},{"link_text":"link","link_src":"http://www.hgtv.com/"},"link_src":"http://www.diynetwork.com/"},"link_src":"http://www.cookingchanel.com/"},"link_src":"http://www.travelchannel.com/"},"link_src":"http://www.food.com/"}]},{"name":"topic2","link_src":"http://www.google.com/"},"link_src":"http://www.yahoo.com/"},"link_src":"http://www.aol.com/"},"link_src":"http://www.msn.com/"},"link_src":"http://www.facebook.com/"},"link_src":"http://www.twitter.com/"}]},{"name":"topic3","link_src":"http://www.a.com/"},"link_src":"http://www.b.com/"},"link_src":"http://www.c.com/"},"link_src":"http://www.d.com/"},"link_src":"http://www.e.com/"},"link_src":"http://www.f.com/"}]}]});

我希望以下列形式:@H_403_2@

主题链接@H_403_2@

尝试一下:
success: function(json) {
   console.log(JSON.stringify(json.topics));
   $.each(json.topics,function(idx,topic){
     $("#nav").html('<a href="' + topic.link_src + '">' + topic.link_text + "</a>");
   });
},
原文链接:https://www.f2er.com/ajax/159901.html

猜你在找的Ajax相关文章