I use ajax with jquery and when I tried to display the result in popup.
Then alert always displays 0
success : function(results) {
alert(results); }
EDIT :
var dataToSend = { action:"ACTION_NAME", id : "12" }; url_action = 'http://www.______.com/wp-admin/admin-ajax.php'; $.ajax({ url: url_action, type:'POST', data: dataToSend, success:function(results) { alert(results); } });
Try to add
die();
orexit();
at the very last line of the function.May be you are trying without login. so you need to use this action.
add_action(‘wp_ajax_nopriv_my_action’, ‘my_action_callback’);
you will get response. 🙂
I had a similar issue with admin-ajax.php returning 0, even i had die().
My problem was that the add_action was declared in a file not loaded default by plugin.
Anyway, if you have problem also check in browser what __.com/wp-admin/admin-ajax.php?action=youraction returns.
Found it.
It need to add
die();
before the end of my own ajax function infunction.php
.Because there is one line of script in
admin-ajax.php
after my own ajax_action that says: die(‘0’); So we need to die() script before die(‘0’).I was adding the “action” incorrectly. You have to prefix your action with
wp_ajax_gd_
.and in the JS jQuery post:
I found WordPress’ documentation very unclear about this part.
That’s probably because the server side script you are calling with AJAX returns
0
(the one you have specified in theurl
property).Make sure the action_name is ok. If it’s not found in functions.php you will get 0 as the result.
In your PHP function, make sure you’re using
echo
instead ofreturn
.This was happening for me in a WordPress theme.
I had the same issue. I’d forgotten to enable the plugin. After enabling the plugin I received the expected data.
Adding
die();
at the end of your function can solve it. Thank you.Those who get error 0 :), action => ‘action’