2014-04-27 81 views
0

我试图使用Disqus API获取评论信息,并且尽可能检索数据,但我在迭代中遇到了很多问题,并且抓住我想要的东西。我检索它看起来像我的浏览器下面的JSON数据:无法迭代通过JSON数据

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

我试图让“raw_message”保持,但我一直沿着线得到错误的无法读取的财产“raw_message”空值。当我尝试使用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); 
      // } 
     } 
    }); 
} 
+0

检查你的json格式在这里http://jsonlint.com/ –

+0

嗯我得到第1行的分析错误,因为“/ **/jQueryRANDOMNUMBERSHER”...任何想法,为什么这显示了? – user2989731

回答

0

它看起来像调用返回一个对象,所以下面应该工作:

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

当故障排除这种类型的东西,你应该在你的回调中设置一个断点,然后简单地查看结果变量包含的内容。

顺便说一句,在响应jQueryRANDOMNUMBERSHER(...)是因为调用作为JSONP请求调用。

0

请确保您有这个

JSONP: “回调” 和数据类型:? '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) {} 

})

,或者你应该能够只是把”回调= ?”在您的网址

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

jQueryRANDOMNUMBERSHERE添加跨域安全原因。