Wordpress - Ajax call always returns 0

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

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

});

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

Tags:

Ajax

Functions