Question

I'm trying to get a hold of comment information using the Disqus API and I've gotten as far as retrieving the data, but I'm having a lot of trouble iterating through it and getting a hold of what I want. I've retrieved JSON data which looks like the following in my browser:

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

I'm trying to get a hold of "raw_message" but I keep getting errors along the lines of Cannot read property "raw_message" of null. When I try using JSON.parse I receive "SyntaxError: Unexpected token 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);
            // }
        } 
    });
 }
Was it helpful?

Solution

It looks like the call returns a single object, so the following should work:

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

When troubleshooting this type of thing, you should set a breakpoint in your callback and then simply look at what the result variable contains.

BTW, the jQueryRANDOMNUMBERSHER(...) in the response is because the call is invoked as a JSONP request.

OTHER TIPS

Make sure you have this

jsonp: "callback" and 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) {}

})

or you should be able to just put "?callback=?" in your url

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

jQueryRANDOMNUMBERSHERE is added for cross domain security reason.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top