0

I have an ajax call in my js file loaded in footer (it actually doesn't matter if I make it simpler or without the json parsing, I will get the same errors) - in this case I pass the callback and any arguments in a function; if I just make this plain and simple it doesn't change:

 function pigafettaGetJSON(func, a) {

    var output = $.parseJSON( $.ajax({
            type: 'POST',
            url : pigafetta.ajax_url,
            data : {
                action : func,
                args : a
            },
            dataType : "json",
            async : false
        }).responseText);

    return output;
}

the ajax url is stored as a variable using wp_localize_script, among others:

    wp_enqueue_script( 'pigafetta', PIGAFETTA_URI . '/assets/scripts/js/app.min.js', array( 'jquery', 'vendor' ), PIGAFETTA_VERSION, true );
    wp_localize_script( 'pigafetta', 'pigafetta', array(
        'ajax_url'      => admin_url( 'admin-ajax.php' ),
        'templates_url' => PIGAFETTA_URI . '/templates/views/',
        'partials_url'  => PIGAFETTA_URI . '/templates/views/partials',
        'i18n'          => pigafetta_phrases()
    ) );

this is the callback function :

function pigafetta_ajax_get_cart() {
    $cart = pigafetta_get_cart();
    wp_send_json_success($cart);
}
add_action( 'wp_ajax_pigafetta_ajax_get_cart', 'pigafetta_ajax_get_cart' );
add_action( 'wp_ajax_nopriv_pigafetta_ajax_get_cart', 'pigafetta_ajax_get_cart' );

this is the function invoked by the ajax callback - which will function fine if called by PHP:

function pigafetta_get_cart() {

    $cart = array();
    $cart['items'] = pigafetta_get_cart_items();
    $count = sizeof( $cart['items'] );
    $cart['count']['items'] = $count;
    $cart['count']['label'] = $count > 0 ? sprintf( _n( '%s item', '%s items', $count, 'pigafetta' ), $count ) : __( 'Cart is empty', 'pigafetta' );
    $cart['empty']['notice'] = $count <= 0 ? __( "You don't have any items yet in your booking.", 'pigafetta' ): '';
    $total = WC()->cart->get_cart_subtotal();
    $cart['totals']['amount'] = $total;
    $cart['totals']['label'] = $count > 0 ? sprintf( _x( 'Total: %s', 'Total amount in cart', 'pigafetta' ), $total ) : '';
    $cart['actions'] = array(
        array(
            'icon' => '<i class="fa fa-shopping-cart"></i>',
            'link'  => WC()->cart->get_cart_url(),
            'label' => __( 'View cart', 'pigafetta' ),
            'description' => __( 'Review your booking', 'pigafetta' ),
            'class' => 'view-cart',
        ),
        array(
            'icon' => '<i class="fa fa-credit-card"></i>',
            'link'  => WC()->cart->get_checkout_url(),
            'label' => __( 'Checkout', 'pigafetta' ),
            'description' => __( 'Finalize your booking', 'pigafetta' ),
            'class' => 'checkout',
        ),
        array(
            'icon' => '<i class="fa fa-trash"></i>',
            'link'  => '',
            'label' => __( 'Empty cart', 'pigafetta' ),
            'description' => __( 'Remove all items from cart', 'pigafetta' ),
            'class' => 'empty-cart',
        )
    );

    return $cart;
}

however, when called with admin-ajax.php in front-size it will return many errors; mostly it will not recognize functions from my own theme; for example, pigafetta_get_cart_items() invokes a another function in my theme... that function is not recognized. But it doesn't make sense, because I don't get the same error if I run these functions from PHP. They have been included in my functions.php file in my theme. If I include again the same function with a require_once inside the function calling the other function it will work but then stopping somewhere else and saying that wp_db_version is an undefined variable, for example.

I don't understand where the error could be, since this is working fine in PHP - my host is WPEngine and I don't have any other issue with them. I double checked this also on my local environment with Vagrant and I get the same errors, including the internal server error.

WP_DEBUG is on but I can't see any relevant information from logs and notices.

I've been spending hours on this and don't know where this comes from...

  • Please, post the server-side code which is generating the error. Also, you say that ajax_url and my_function_callback are correctly defined but we can't see that, can you post the code where you have defined that variables? – cybmeta Dec 9 '14 at 12:44
  • the ajax url is in a variable printed after using wp localize script immediately after my script is enqueued - I double checked that from console.log and it's fine – unfulvio Dec 9 '14 at 12:46
  • the function used as ajax callback is the simplest ever: $stuff = some_php_function(); wp_send_json_success($stuff); I don't think I can post the whole php function as it's very very long - but this function is also used server side and doesn't return any error – unfulvio Dec 9 '14 at 12:48
  • Please, edit your question and post the code you are using, otherwise we can't help. – cybmeta Dec 9 '14 at 12:49
  • ok I just did; somehow it's like internal functions of wordpress and some plugins pass, but some defined in my theme don't... although I can't explain it since they are all included in functions.php and from PHP they work – unfulvio Dec 9 '14 at 13:00

Your Answer

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

Browse other questions tagged or ask your own question.