无法遍历JSON数据

时间:2014-04-27 00:07:49

标签: ajax json disqus

我试图使用Disqus API获取评论信息并且我已经获得了检索数据,但是我在迭代它并获得很多麻烦抓住我想要的东西。我已经在浏览器中检索了如下所示的JSON数据:

 /**/ jQueryRANDOMNUMBERSHERE({"code":0,"response":{"parent":null,"likes":0, "raw_message": "Lorem ipsum dolor sit amet, consectetur adipisicing elit"}});

我试图抓住" raw_message"但是我一直都会遇到无法读取属性的错误" raw_message"为null。当我尝试使用JSON.parse时,我收到"语法错误:意外的令牌o"

 function showFeaturedComments() {
    jQuery.ajax({
        type: 'GET',
        url: "https://disqus.com/api/3.0/posts/details.json",
        data: { api_key: disqusPublicKey, post: idArray },
        cache: false,
        dataType: 'jsonp',
        success: function(result) {

            // var parseResults = JSON.parse(result);
            // console.log(parseResults);
            // var disqusResults = result.response;

            // jQuery.each(result.response, function() {
              // console.log(response.raw_message);
            // });

            // for (var i in result.response) {
            //     console.log(result.response[i].raw_message);
            // }
        } 
    });
 }

2 个答案:

答案 0 :(得分:0)

看起来调用返回一个对象,因此以下内容应该有效:

success: function(result) {
    console.log(result.response.raw_message);
}

在对此类问题进行故障排除时,您应该在回调中设置断点,然后只查看结果变量包含的内容。

BTW,响应中的jQueryRANDOMNUMBERSHER(...)是因为调用是作为JSONP请求调用的。

答案 1 :(得分:0)

确保你有这个

jsonp:“callback”和dataType:'jsonp',

jQuery.ajax({
    type: 'GET',
    url: "https://disqus.com/api/3.0/posts/details.json",
    data: { api_key: disqusPublicKey, post: idArray },
    cache: false,
    dataType: 'jsonp',
    jsonp: "callback",
    success: function(result) {}

})

或者你应该只能把“?callback =?”在你的网址

https://disqus.com/api/3.0/posts/details.json?callback=jQueryRANDOMNUMBERSHERE

为了跨域安全原因,添加了jQueryRANDOMNUMBERSHERE。

相关问题