为什么AJAX JSON脚本返回额外的0(零) [英] Why does AJAX json script return extra 0 (zero)

查看:110
本文介绍了为什么AJAX JSON脚本返回额外的0(零)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX功能,在Word preSS调用一个PHP函数返回一个瞬态记录的值在数据库中。

I have an AJAX function in WordPress that calls a PHP function to return the value of a transient record in the Database.

当我打电话使用jQuery的功能,我收到了结果,但是始终有一个额外的0(零)附加的价值。

When I call the function using jQuery, I receive the result however it always has an extra 0 (zero) appended to the value.

下面是我的jQuery的功能:

Here is my jQuery function:

(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);  
                    }
            }
        };

    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');
                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);

有涉及然而这是主要的jQuery函数与Word preSS进行通信,并返回PHP函数的值。更辅助函数

There are more helper functions involved however this is the main jQuery function that communicates with WordPress and returns the value of the PHP function.

问题是,如果返回值是 99 例如,它会返回 990

The issue is that if the value is returned as "99" for example it will be returned as "990"

下面是PHP函数,jQuery的呼吁:

/**
* Get Facebook Likes
*/

public function get_facebook_likes(){

    echo 99;
}

如果我改变上面返回99; 我收到普通的0

If I change the above to return 99; I receive plain 0

推荐答案

您的函数应使用 wp_send_json 为en code中的PHP作为JSON并将其发送回AJAX请求处理程序。这也将停止执行以下任何PHP也一样,所以没有必要使用退出或死亡。

Your function should use wp_send_json to encode the PHP as JSON and sent it back to the AJAX request handler. This will also stop executing of any following PHP too, so there is no need to use exit or die.

因此​​,对于你具体的例子,你可以使用:

So for your specific example, you would use:

/**
* Get Facebook Likes
*/

public function get_facebook_likes(){
   wp_send_json(99);
}

这篇关于为什么AJAX JSON脚本返回额外的0(零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆