6

I am creating a simple wordpress plugin and trying to use AJAX, but I always get 0 in ajax response.

<script type="text/javascript" >
jQuery(document).ready(function($) {

var data = {
    action: 'my_action',
    whatever: '1234'
};


jQuery.post("http://localhost/taichi/wp-admin/admin-ajax.php", data,   function(response) {
    alert(response);
});
});
 </script>
<?php
add_action('wp_ajax_my_action', 'my_action_callback');
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' ); 




function my_action_callback() {

echo "test";
die();

}

what am I doing wrong?

Waqar
  • 101
  • 2
  • 11
  • Maybe this answer can help you [Why is my ajax request getting response 0?][1] [1]: http://stackoverflow.com/a/19371705/3248921 – athivvat Mar 11 '15 at 04:02

5 Answers5

1

Try to change :

jQuery.post("http://localhost/taichi/wp-admin/admin-ajax.php", data,   function(response)

To :

jQuery.post(ajaxurl, data, function(response) 

And check if it is working on the admin side first. It should work fine.

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
1

You have to put the add_action at the complete bottom of your file or else it won't find the callback function

Alexcp
  • 348
  • 1
  • 6
0

Had the same problem, it turned out that my callback was inside a php file which was only included to my "Theme Options" page.

To check if the function is able to trigger trougth admin-ajax.php try to add var_dump(function_exists("your_callback_name")); to the bottom of the wp-admin/admin-ajax.php (before die( '0' );) and then have a look to your ajax output.

artnikpro
  • 5,487
  • 4
  • 38
  • 40
0

Try the following code in your plugin file. or in function.php


    
    jQuery(document).ready(function($){
    var ajaxURL = 'http://localhost/taichi/wp-admin/admin-ajax.php';
    var dataString = 'action=mnd_news';
    $.ajax({
    type: "POST",
    url: ajaxURL,
    data: dataString,
    cache: false,
    success: function(response){
    if(response != 'error') {
    alert(response);
    }
    }
    });
    });
    
    add_action('wp_ajax_mnd_news', 'get_mnd_ajax');
    add_action( 'wp_ajax_nopriv_mnd_news', 'get_mnd_ajax' ); 
    function get_mnd_ajax() {
    echo "test";
    die();
    }

0

Error Return Values

If the AJAX request fails when the request url is wp-admin/admin-ajax.php, it will return either -1 or 0 depending on the reason it failed.

Read this

Edit

admin-ajax always return default '0' as output.so while you alerting response you will 0 only.using die() in callback function will terminate that.

Gowri
  • 16,587
  • 26
  • 100
  • 160
  • but why is it failing? is there any way to figure it out? – Waqar Jul 12 '11 at 03:51
  • I am using die(); in callback function ------> function my_action_callback() { echo "test"; die(); } – Waqar Jul 12 '11 at 04:26
  • maybe my callback function is not being called for some reason. I tried using this code in my plugin and on my theme's index.php file but no luck. – Waqar Jul 12 '11 at 04:35
  • @Waqar: Similar problem here http://wordpress.org/support/topic/plugin-ajax-call-to-wp-ajaxphp-in-admin-page-returning-0 . he use some numeric at the end of callback,But i am not sure – Gowri Jul 12 '11 at 04:40
  • I tried that as well but no luck but thanks for sending the link. – Waqar Jul 12 '11 at 04:58
  • Using `die` just results in nothing being returned, not a proper return value. – ProfK Dec 30 '11 at 10:44
  • Profk, try using die($returnSomething); The example is not very straight forward, is there a way to debug or return a reason why it's failing? – Steve C Sep 25 '12 at 05:58