0

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

axelra82
  • 517
  • 8
  • 23
  • http://stackoverflow.com/questions/9887009/how-do-i-iterate-through-this-json-object-in-jquery That might help, Google for "iterate through json object jquery" You should find your answer in that search – Rick Calder Aug 07 '13 at 12:40
  • @RickCalder Thank you for your suggestion. But I'm not trying to retrieve any lists of objects/values. I just want to extract the one name and the complete message written. – axelra82 Aug 07 '13 at 12:55
  • Right but it's still being returned as a JSON object, even if there is only one item in the array you still need to get it out of the object. – Rick Calder Aug 07 '13 at 12:56

2 Answers2

0

you need to provide success callback, in order to use the returned json data.

$.ajax({
     type: 'POST',
     url: 'URL_OF_MAIL.PHP',
     data: {
         'authname': authname,
         'authmail': authmail,
         'link': link,
         'disqusCall': disqusCall
     },
     cache: false,
     success: function (data) {
         if(data.length>0)
         {
             //read the json data
         }
     }

 });
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • Thank you for your answer @Ravi. How do I "read" the JSON data? What I'm trying to get here is, change the "'disqusCall': disqusCall" to 'name': name and 'message': message. So that I can add the acctuall message and poster name in the email. What would I put under "//read the json data"? And is this the correct place to add these values? – axelra82 Aug 07 '13 at 12:49
0

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.

axelra82
  • 517
  • 8
  • 23