I am relatively new to jQuery and AJAX in particular. I have a small issue with the return value always being 0, though I think this is actually the success message and it’s not returning anything.
I have scoured the Google-verse and I have the die() function on the PHP callback and I believe the add_actions are correct.
I am working on a local host, though I doubt that affects it and this is all in the admin, not front end. I also checked that the js is enqueued and localised.
I get a 200 OK message in the chrome developer area.
I also tested out the basic AJAX from http://codex.wordpress.org/AJAX_in_Plugins and it also returned 0, which makes me wonder if it is something other than the code outlined below.
Right now I am just trying to make it send something back to the jQuery. Any help would be appreciated.
The jQuery
jQuery(document).ready(function(){
jQuery('.cl_link_buttons').val('id').click(function() {
var currentid = jQuery(this).attr('id');
//alert(currentid);
console.log(currentid);
jQuery.ajax ( data = {
action: 'cleanlinks_ajax_get_post_data',
url: ajaxurl,
type: 'POST',
dataType: 'text',
"currentid" : currentid
});
jQuery.post(ajaxurl, data, function(response) {
var dataz = response;
alert( dataz );
console.log (dataz); //show json in console
});
return false;
}); //end click event
}); //end doc ready
The PHP
add_action("wp_ajax_cleanlinks_ajax_get_post_data", "cleanlinks_ajax_get_post_data");
add_action("wp_ajax_nopriv_cleanlinks_ajax_get_post_data", "cleanlinks_ajax_get_post_data");
function cleanlinks_ajax_get_post_data() {
$from_ajax = $_POST['currentid'];
echo "do" . $from_ajax . "something";
die();
}
A
0
response means either that the action is not set (in the ajax data) or that the action’s callback function cannot be found.What you have to do is add
die();
at the end of your function.See the reason and more here: http://codex.wordpress.org/AJAX_in_Plugins
Notes:
echo
something before executingdie
. This will prevent server errors, and will help when debugging.I had this problem too, and it was the fact that I was using
return
instead ofecho
in my PHP function. Changing it toecho
fixed it.So I worked it out. It was not the jQuery as such though I have improved that, it was the placement of the call back function. I moved it over to the main plugin file and it worked.
I got same problem. And solved it. You must send “action” variable like in example:
Because in wp-admin/admin-ajax.php is handler for action variable:
Try running this code on the console
I can see many things wrong about your JavaScript code and that might be the reason.
//code function php
Just for reference, anybody coming from shortcode development, if you are getting a proper response through WordPress Ajax request but a 0 is getting appended, it’s only because you are ‘echo’ing instead of ‘return’ing. Shortcodes are never meant to ‘echo’ or output anything. Just another scenario.
I had the same problem, to fix it I used
wp_die()
at the end of my function just after anecho
. Don’t forget to pass your action on your script.To be sure, check if your function has to use
wp_ajax_nopriv
likewp_ajax
.Just for reference, for anyone who get here googling “ajax request is returning 0”:
Remember when you add ajax action to object’s method to be sure methods access modifier is
public
.add_action
just silences if it can’t call your method outside of$object
.If you don’t use wp_localize_script() function to set ajax url, admin ajax returns 0. I think it’s WordPress bug. Here’s is an example :
The javascript file (search.js) :
Those who get error 0 :), action => ‘action’
If you are using localhost and your php server side code is in a plugin file first login to admin dashboard and refresh the plugin page. Secondly, check if the plugin is activated. Then go to frontend and refresh and try sending again.
Hope it is helpful for you,
Best
Try adding an
if
statement: