Why AJAX json script returns extra 0 (zero)

I have an AJAX function in WordPress that calls a PHP function to return a transition entry value to a database.

When I call a function using jQuery, I get the result, but always has an extra 0 (zero) added to the value.

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 data element.data('advanceddashboardwidget', advanceddashboardwidget); }); }; }); })(jQuery); 

There are more helper functions, but this is the main jQuery function, which communicates with WordPress and returns the value of the PHP function.

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

Here is the PHP function that jQuery calls:

 /** * Get Facebook Likes */ public function get_facebook_likes(){ echo 99; } 

If I change the above to return 99; I get a simple 0

+4
source share
3 answers

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

So, for your specific example, you should use:

 /** * Get Facebook Likes */ public function get_facebook_likes(){ wp_send_json(99); } 
+6
source

This is an old question, but I'm going to answer that. The wp_send_json() function may help, but not always. There may be some moments when you cannot use this function. For example, when you download messages using ajax, you get part of the template, you can use this function. Therefore, the WordPress documentation suggests using the die() function.

So your php function should look like this:

 /** * Get Facebook Likes */ public function get_facebook_likes() { echo 99; die(); } 
+1
source

Use Firebug and view the actual network data sent and received. Determine if the error comes from JavaScript or PHP. Copy the network request and paste it into a separate browser window to see the original result. If this is PHP, continue this. if this javascript does something, let us know.

0
source

Source: https://habr.com/ru/post/1448030/


All Articles