Search code examples
jqueryajaxjsonapidisqus

Retrieve JSON information? Jquery


OK, this might seem like a dumb question, but please keep in mind that JSON is completely new to me (I've heard the expression before. But know nothing about it).

I have this callback function to notify authors on a site with an email, when new comments are added to the disqus thread.

<script type="text/javascript">
    function disqus_config() {
        this.callbacks.onNewComment = [function(comment) {

            var authname = document.getElementById('authname').value;
            var authmail = document.getElementById('authmail').value;
            var link = document.getElementById('link').value;
            var disqusAPIKey = 'MY_API_KEY';
            var disqusCall = 'https://disqus.com/api/3.0/posts/details.json?post=' + comment.id + '&api_key=' + disqusAPIKey;

            $.ajax({
                type: 'POST',
                url: 'URL_OF_MAIL.PHP', 
                data: {'authname': authname, 'authmail': authmail, 'link': link, 'disqusCall': disqusCall},
                cache: false, 
            });
        }];
    }
</script>

Everything works like a charm. Except... What is outside the scope of my understanding is (and I've searched around. But seeing as I don't know anything about JSON, I don't even really know what to look for) how to extract the information from 'disqusCall' variable? As it sits now, I just get a link (that contains two things I'm interested in, name and message). I would like to include these in the mail message.

I'm sure this is something simple as "decoding" the JSON information, but I don't know how. And all the posts I've found on this subject have just confused me even more haha


Solution

  • So I was able to get this working with the help of a friend who has some better knowledge of JSON.

    This is what I ended up with

    <script type="text/javascript">
        function disqus_config() {
            this.callbacks.onNewComment = [function(comment) {
    
                var authname = document.getElementById('authname').value;
                var authmail = document.getElementById('authmail').value;
                var link = document.getElementById('link').value;
                var disqusAPIKey = 'MY_API_KEY';
    
                $.ajax({
                    type: 'GET',
                    url: 'https://disqus.com/api/3.0/posts/details.json',
                    data: {
                        'post': comment.id,
                        'api_key': disqusAPIKey
                    },
    
                    success: function (data) {
                        var post_author_name = data.response.author.name;
                        var comment = data.response.raw_message;
    
                        $.ajax({
                            type: 'POST',
                            url: 'URL_TO_MAIL.PHP',
                            data: {
                                'authname': authname,
                                'authmail': authmail,
                                'link': link,
                                'post_author_name': post_author_name,
                                'comment': comment
                            },
                        });
                    },
                    cache: false,
                });             
            }];
        }
    </script>
    

    You can view an article I wrote about this here. It describes what I was using the JSON for.