Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does AJAX json script return extra 0 (zero)
    text
    copied!<p>I have an AJAX function in WordPress that calls a PHP function to return the value of a transient record in the Database.</p> <p>When I call the function using jQuery, I receive the result however it always has an extra 0 (zero) appended to the value.</p> <p>Here is my jQuery function:</p> <pre><code>(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); </code></pre> <p>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.</p> <p>The issue is that if the value is returned as "<code>99</code>" for example it will be returned as "<code>990</code>"</p> <p><strong>Here is the PHP function that jQuery is calling:</strong></p> <pre><code>/** * Get Facebook Likes */ public function get_facebook_likes(){ echo 99; } </code></pre> <p>If I change the above to <code>return 99;</code> I receive plain 0</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload