Ajax call always returns 0

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!!

Read More

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

Related posts

6 comments

  1. 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);
      } 
    
    });
    
  2. 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(); 
    }
    
  3. 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();
    }
    
  4. 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.

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

Comments are closed.