26

I have a problem with AJAX returning 0 always!

I have done everything by the book and can't figure out what is wrong? Please help!!

Here is my Ajax call:

//Pass data through AJAX
var amountToConvert = price;

jQuery.ajax({
      type:"POST",
      url: "../../wp-admin/admin-ajax.php", // our PHP handler file
      action: "ajaxConversion",
      data: {
          amount: amountToConvert
      },
      success:function(data){
      alert(data);
      },
      error: function(errorThrown){
          alert(errorThrown);
      } 

  });
return false;

And the function in functions.php is:

function ajaxConversion(){

$amount = mysql_real_escape_string($_POST['amount']);

echo $amount;

die();
};

add_action('wp_ajax_nopriv_ajaxConversion', 'ajaxConversion');
add_action('wp_ajax_ajaxConversion', 'ajaxConversion');
1
  • Have you tried to debug your code? Also use admin_url( 'admin-ajax.php' ) to get the AJAX URL, not some made-up URL.
    – fuxia
    Aug 30, 2013 at 10:26

7 Answers 7

16

Could you place the action (ajaxConversion) in your Data and check?

jQuery.ajax({
  type:"POST",
  url: ajaxurl,
  data: {
      action: "ajaxConversion",
      amount: amountToConvert
  },
  success:function(data){
  alert(data);
  },
  error: function(errorThrown){
      alert(errorThrown);
  } 

});
3
  • This is still appending a 0 to my content somehow. Mar 26, 2014 at 15:55
  • 1
    @BenRacicot : Can you check if you have any statements that does output like echo, die(), etc in your PHP code? Mar 27, 2014 at 3:26
  • 3
    Hey Jay, Turns out adding die(); to my ajax func fixed it! Mar 27, 2014 at 13:46
30

using wp_die(); at the end of AJAX function fixed the issue for me.

e.g

add_action( 'wp_ajax_my_ajax_function', 'my_ajax_function' );

function my_ajax_function(){      
    echo json_encode($myvar);
    wp_die(); 
}
1
  • thanks... dont know why but using just die() didnt work.
    – Sagive
    Jul 12, 2015 at 8:38
12

For me the trick was to add wp_ajax_nopriv action. I tested the script on one browser when I was logged in WP admin, and then I tried same script in Chrome and realized that the script doesn't work. After I put wp_ajax_nopriv, everything started to work. :)

add_action( 'wp_ajax_nopriv_erase_uploaded_images', 'erase_uploaded_images' );
add_action( 'wp_ajax_erase_uploaded_images', 'erase_uploaded_images' );

function erase_uploaded_images() {
    $attach_id = filter_input( INPUT_POST, 'attach_id' );
    wp_delete_attachment( $attach_id );
    if ( isset( $_SESSION['uploaded_images'] ) ) {
        $array_attachments = $_SESSION['uploaded_images'];
        if ( ( $key = array_search( $attach_id, $array_attachments ) ) !== false ) {
            unset( $array_attachments[$key] );
        }
        $_SESSION['uploaded_images'] = $array_attachments;
    }
    wp_die();
}
2
  • This, my friend, has saved me 2 hours of frustrating. Another lesson to learn when deploy your application to another environment T_T Jun 1, 2017 at 3:49
  • This is very true...if you're logged in to admin then _nopriv function is not called Nov 7, 2017 at 13:48
10

I would recommend using wp_send_json_success() and wp_send_json_error() on server side. You don't need to worry about die() etc and the "status" variable is sent automatically, it's much cleaner this way. For example

function ajaxConversion(){
    // ...
    wp_send_json_success(array(
      'amount' => $amount
    ));
}

Will result in something like this:

{
 "success":true,
 "data":{"amount":125}
}

So then you can do easily extract the values in your ajax call:

jQuery.ajax({
    type       : 'post',
    data       : {
                  action: 'ajaxConversion', 
                  //nonce : ajax.nonce                              
                 },
    dataType   : 'json',
    url        : ajax.ajaxurl,
    success    : function(data){
         if(data.success) {
            alert(data.amount);                         
         } else {
            alert(data.data.message);
         }

    }
}); 

Another common thing i've ran into are typos in the action name. They should be wp_ajax_nopriv_{action} or wp_ajax_{action} when logged in. For example, wp-ajax_nopriv, is one I've done several times in the past.

1
  • 1
    this right here should be the correct and accepted answer. amazing!
    – eballeste
    Feb 11, 2020 at 17:32
1

For me it was the fact that I was using return instead of echo in my PHP function. Changing it to echo fixed it.

function doAjax() {
    $result = getPosts();
    echo json_encode($result, true);
    die();
}
0

You may forget to add below line:

add_action("wp_ajax_your_ajax_function", "your_ajax_function");

Without above line, the ajax it not be fired.

Here is your ajax function

function your_ajax_function(){ $result = json_encode($result); echo $result; }

0

important think do,hook action correctly add function.php i face same issues ,i try this all methods,but simple think i missed require my action file to functions.php

add_action( 'wp_ajax_nopriv_erase_uploaded_images', 'erase_uploaded_images' );
add_action( 'wp_ajax_erase_uploaded_images', 'erase_uploaded_images' );

function erase_uploaded_images() {
    $attach_id = filter_input( INPUT_POST, 'attach_id' );
    wp_delete_attachment( $attach_id );
    if ( isset( $_SESSION['uploaded_images'] ) ) {
        $array_attachments = $_SESSION['uploaded_images'];
        if ( ( $key = array_search( $attach_id, $array_attachments ) ) !== false ) {
            unset( $array_attachments[$key] );
        }
        $_SESSION['uploaded_images'] = $array_attachments;
    }
    wp_die();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.