Javascript 使用WordPress瞬态缓存和AJAX json脚本时出现的问题

Javascript 使用WordPress瞬态缓存和AJAX json脚本时出现的问题,javascript,json,wordpress,jsonp,transient,Javascript,Json,Wordpress,Jsonp,Transient,我有一个我正在开发的WordPress插件,它可以对所有主要的社交网站进行民意调查,并返回特定用户的社交数量(关注者) 这在服务器上可能非常缓慢和密集,因此我使用WordPress瞬态缓存构建了插件来存储从社交网络站点返回的详细信息,并且我还使用jQuery AJAX json来显示数据 (function($) { $(document).ready( function() { var AdvancedDashboardWidget = function(elemen

我有一个我正在开发的WordPress插件,它可以对所有主要的社交网站进行民意调查,并返回特定用户的社交数量(关注者)

这在服务器上可能非常缓慢和密集,因此我使用WordPress瞬态缓存构建了插件来存储从社交网络站点返回的详细信息,并且我还使用jQuery AJAX json来显示数据

(function($) {
    $(document).ready( function() {

        var AdvancedDashboardWidget = function(element, options)
        {
            var ele = $(element);
            var settings = $.extend({
                action: '',
                service: '',
                countof: '',
                query:   '',
                callback:''
            }, options || {});
            this.count=0;
            var url='';
            switch(settings.service)
            {
                case 'facebook':
                    if(settings.countof=='likes' || settings.countof=='talks')
                    {
                        ajaxCall(action,ele,settings);  
                    }
                    break;
            }
        };

    var ajaxCall = function(action,ele,settings){
        opts = {
            url: ajaxurl, // ajaxurl is defined by WordPress and points to /wp-admin/admin-ajax.php
            type: 'POST',
            async: true,
            cache: false,
            dataType: 'json',
            data:{
                action: settings.action // Tell WordPress how to handle this ajax request
            },
            success:function(response) {
                //alert(response);
                ele.html(response);
                return; 
            },
            error: function(xhr,textStatus,e) {  // This can be expanded to provide more information
                alert(e);
                //alert('There was an error deleting the cache');
                return; 
            }
        };
        $.ajax(opts);
    };


        $.fn.advanceddashboardwidget = function(options)
        {
            return this.each(function()
            {
                var element = $(this);

                // Return early if this element already has a plugin instance
                if (element.data('advanceddashboardwidget')) return;

                // pass options to plugin constructor
                var advanceddashboardwidget = new AdvancedDashboardWidget(this, options);

                // Store plugin object in this element's data
                element.data('advanceddashboardwidget', advanceddashboardwidget);
            });
        };

    });
})(jQuery);
这些是主要功能:

检索Facebook计数

/**
 * Fetch Facebook count.
 *
 * @param string $url The url to fetch.
 * @return int of Facebook counts.
 */
function ass_get_fb_likes($facebook_id) {

    try {
        $json = wp_remote_get("http://graph.facebook.com/".$facebook_id);

        if(is_wp_error($json))
            return false;

        $fbData = json_decode($json['body'], true);
        return format(intval($fbData['likes']));

    } catch (Exception $e) {
        return false;
    }
}
上述函数还连接到另一个处理瞬态缓存的函数。这方面效果很好

处理社交网络数据的初始显示

jQuery(function($) {

    $('#fblikes').advanceddashboardwidget({
        'action':'get_facebook_likes',
        'service':'facebook',
        'countof':'likes',
        'callback':'formatCount'
    });
});
设置显示格式的辅助功能

function formatCount(element,count){
    var display_count='';
    count=parseInt(count,10);
    if(count>1000000)
    {
        count=count/1000000;
        count=count.toFixed(0);
        display_count=count+'m';
    }
    else if(count>1000)
    {
        count=count/1000;
        count=count.toFixed(0);
        display_count=count+'k';
    }
    else
    {
        display_count=count;
    }
    element.html(display_count);
}
下面的函数,如果给我一个问题。它用于与WordPress通信,以调用PHP函数并检索数据

(function($) {
    $(document).ready( function() {

        var AdvancedDashboardWidget = function(element, options)
        {
            var ele = $(element);
            var settings = $.extend({
                action: '',
                service: '',
                countof: '',
                query:   '',
                callback:''
            }, options || {});
            this.count=0;
            var url='';
            switch(settings.service)
            {
                case 'facebook':
                    if(settings.countof=='likes' || settings.countof=='talks')
                    {
                        ajaxCall(action,ele,settings);  
                    }
                    break;
            }
        };

    var ajaxCall = function(action,ele,settings){
        opts = {
            url: ajaxurl, // ajaxurl is defined by WordPress and points to /wp-admin/admin-ajax.php
            type: 'POST',
            async: true,
            cache: false,
            dataType: 'json',
            data:{
                action: settings.action // Tell WordPress how to handle this ajax request
            },
            success:function(response) {
                //alert(response);
                ele.html(response);
                return; 
            },
            error: function(xhr,textStatus,e) {  // This can be expanded to provide more information
                alert(e);
                //alert('There was an error deleting the cache');
                return; 
            }
        };
        $.ajax(opts);
    };


        $.fn.advanceddashboardwidget = function(options)
        {
            return this.each(function()
            {
                var element = $(this);

                // Return early if this element already has a plugin instance
                if (element.data('advanceddashboardwidget')) return;

                // pass options to plugin constructor
                var advanceddashboardwidget = new AdvancedDashboardWidget(this, options);

                // Store plugin object in this element's data
                element.data('advanceddashboardwidget', advanceddashboardwidget);
            });
        };

    });
})(jQuery);
问题

问题在于,当从瞬态函数返回数据时,该数字总是附加一个额外的0(零)。从我所阅读的内容来看,这可能是因为我使用的是“
json
”而不是“
jsonp

当我将其更改为“
jsonp
”时,“我得到一个错误”错误:jquery17201280598581866697_1353705456268未被调用。我想这与回调函数有关

到目前为止,我发现这是在网站上显示这些信息的最快方式。如果数据存在于临时缓存中,页面加载速度很快,但如果不存在,则可能需要几秒钟的时间,这就是我希望jQuery进入的地方,可能会显示加载图形,直到检索到数据为止


任何帮助都将不胜感激。

在您将AJAX数据返回AJAX函数之前,您需要
die()
,否则将由WordPress'完成,它以
die('0')
结尾

编辑: WordPress现在(从3.5.0开始)有以下功能:

wp\u发送json\u成功($data)


wp\u send\u json\u error($data)
:。

额外的0可能是由于没有退出返回json数据的方法造成的。总是让我抓狂。