jquery - WordPress AJAX return 0 - My case

My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions

My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions and good practices, but it does not work. Here my code:

jQuery("#genera-pdf").click(function(e){
  e.preventDefault();
  var price1 = jQuery('input[type=text][id=price1]').val();
  var price2 = jQuery('input[type=text][id=price2]').val();

   jQuery.ajax({
               url : '/wp-admin/admin-ajax.php',
               type: 'POST',
               data: {action: 'pdfAction', a: price1, b: price2}),
               success: function(data){
                   alert(data);     
               },
               error: function(errorThrown){
                   alert(errorThrown);
               }
             });
          });

And my function is:

function pdfAction(){
$post = $_POST['pdfAction'];
echo "something";

wp_die();

}
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

Any idea that is happening?

My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions and good practices, but it does not work. Here my code:

jQuery("#genera-pdf").click(function(e){
  e.preventDefault();
  var price1 = jQuery('input[type=text][id=price1]').val();
  var price2 = jQuery('input[type=text][id=price2]').val();

   jQuery.ajax({
               url : '/wp-admin/admin-ajax.php',
               type: 'POST',
               data: {action: 'pdfAction', a: price1, b: price2}),
               success: function(data){
                   alert(data);     
               },
               error: function(errorThrown){
                   alert(errorThrown);
               }
             });
          });

And my function is:

function pdfAction(){
$post = $_POST['pdfAction'];
echo "something";

wp_die();

}
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

Any idea that is happening?

Share Improve this question asked Jun 11, 2019 at 14:48 Mirquez RodriguezMirquez Rodriguez 31 bronze badge 2
  • Have you tried using the REST API for ajax instead? It's a much better API and actually tells you what the problem is in plain english, rather than returning 0 with no explanation – Tom J Nowell Commented Jun 11, 2019 at 15:06
  • Please, how I do this? – Mirquez Rodriguez Commented Jun 19, 2019 at 17:03
Add a comment  | 

1 Answer 1

Reset to default 2

You function name and your ajax action callback name should be same.

add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

function pdfAction_callback(){
    $post = $_POST['pdfAction'];
    echo "something";

    wp_die();

}

Please update your php code with the above.

Currently, your php function is not getting executed.

Update:

The Ajax Script,

$(document).on('click', "#generate-pdf", function(){

    var price1 = $("#price1").val();
    var price2 = $("#price2").val();

    $.ajax({
        url: ajax_object.ajax_url, // AJAX handler
        data: {
            'action': 'pdfAction',
            'price_1': price1,
            'price_2': price2
        },
        type: 'POST',
        success: function(data) {

            console.log(data);
            console.log(data.price_1) // to access individual values

        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
    return false;

});

Register Ajax Action Url and Script,

function ajax_enqueue_scripts() {

    // Enqueue your javascript file 
    wp_register_script( 'ajax-scripts', get_template_directory_uri() . '/js/ajax-scripts.js', array('jquery'), '1.0.0', true );

    // in JavaScript, object properties are accessed as ajax_object.ajax_url
    wp_localize_script( 'ajax-scripts', 'ajax_object', array( 
        'ajax_url' => admin_url( 'admin-ajax.php' ), // admin ajax url 
    ));

    wp_enqueue_script('ajax-scripts');

}

add_action( 'wp_enqueue_scripts', 'ajax_enqueue_scripts' );

Ajax Request Callback function,

add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

function pdfAction_callback(){

    // Getting data sent from Ajax request

    $price_1 = $_POST['price_1'];
    $price_2 = $_POST['price_2'];


    // Prepare Data to be send back to AJAX request
    $return = array(
        'price_1'  => $price_1,
        'price_2'  => $price_2
    );

    wp_send_json($return); // Send JSON response back to our AJAX Request and die().

    // we don't require wp_die(); here wp_send_json() will take care of it.

}

You also should consider using nonce here for security purpose.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745412819a4626608.html

相关推荐

  • jquery - WordPress AJAX return 0 - My case

    My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信