15

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');
  • 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 '13 at 10:26
14

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);
  } 

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

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(); 
}
  • thanks... dont know why but using just die() didnt work. – Sagive SEO Jul 12 '15 at 8:38
9

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();
}
  • This, my friend, has saved me 2 hours of frustrating. Another lesson to learn when deploy your application to another environment T_T – Tree Nguyen Jun 1 '17 at 3:49
  • This is very true...if you're logged in to admin then _nopriv function is not called – Michal Holub Nov 7 '17 at 13:48
2

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.

0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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