【问题标题】:Retrieve JSON information? Jquery检索 JSON 信息? jQuery
【发布时间】:2013-08-08 20:06:46
【问题描述】:

好吧,这似乎是一个愚蠢的问题,但请记住 JSON 对我来说是全新的(我以前听过这个表达。但对此一无所知)。

当新的 cmets 添加到 disqus 线程时,我有这个回调函数可以通过电子邮件通知网站上的作者。

<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>

一切都像一个魅力。除了...超出我理解范围的是(我已经四处搜索。但是看到我对 JSON 一无所知,我什至不知道要寻找什么)如何提取信息来自“disqusCall”变量?现在,我只得到一个链接(其中包含我感兴趣的两件事,名称和消息)。我想在邮件中包含这些内容。

我确信这只是“解码”JSON 信息的简单方法,但我不知道如何操作。我发现的所有关于这个主题的帖子都让我更加困惑哈哈

【问题讨论】:

  • stackoverflow.com/questions/9887009/… 这可能会有所帮助,谷歌“遍历 json 对象 jquery”你应该在那个搜索中找到你的答案
  • @RickCalder 谢谢你的建议。但我不想检索任何对象/值列表。我只想提取一个名字和写的完整消息。
  • 对,但它仍然作为 JSON 对象返回,即使数组中只有一项,您仍然需要将其从对象中取出。

标签: jquery ajax json api disqus


【解决方案1】:

因此,在一位对 JSON 有更深入了解的朋友的帮助下,我得以完成这项工作。

这就是我的结局

<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>

您可以查看我写的关于此here 的文章。它描述了我使用 JSON 的目的。

【讨论】:

    【解决方案2】:

    您需要提供成功回调,才能使用返回的 json 数据。

    $.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。如何“读取” JSON 数据?我想要在这里的是,将“'disqusCall':disqusCall”更改为“name”:name 和“message”:message。这样我就可以在电子邮件中添加实际消息和海报名称。我会在“//读取 json 数据”下放什么?这是添加这些值的正确位置吗?
    猜你喜欢
    • 2013-08-28
    • 2017-05-23
    • 2021-05-03
    • 2011-03-26
    • 2014-07-02
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多