加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – 使用jQuery解析JSON响应

发布时间:2020-12-16 01:35:51 所属栏目:百科 来源:网络整理
导读:我在我的一个应用程序中处理 JSON响应.我已成功使用jsonp建立连接.但是我无法解析我的回复. 码: script type='text/javascript'(function($) {var url = 'http://cooktv.sndimg.com/webcook/sandbox/perf/topics.json';$.ajax({ type: 'GET',url: url,async
我在我的一个应用程序中处理 JSON响应.我已成功使用jsonp建立连接.但是我无法解析我的回复.

码:

<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>

输出我得到:

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

响应示例:

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/"}]}]});

我希望以下列形式:

主题:链接

尝试一下:
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>");
   });
},

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读