user1232551
user1232551

Reputation: 53

Add_action hook not triggered, so Ajax returns Undefined

It is my first time implementing ajax. I have been grappling with this sample ajax code for a couple of days, searched in stackoverflow, read blogs, tried many different solutions, even found some errors, but.. the code still return a $response = Undefined/0.

Wordpress uploads ajax in the page, the page interacts with the server with an ajax call and returns success, but the variable $response is not passed to jquery, neither response.status nor response.message.

In fact, $response is Undefined or 0 in the Firefox Console.

**After all those check, I posted this question. Snake_Eyes and Froxz found some errors that I corrected. Nevertheless, ajax still returns Undefined for $response.

I have investigate whether Wordpress calls do_stuff_pp(). To do so, I have checked the do_stuff_pp() function line by line using PHP function error_log(). The analysis shows that the do_stuff_pp() function never runs. But I do not know why: :(**

I am using jquery 1.5.1, WP 2.9.2, and php 5.2.6

function my_init() {

    switch ($page_num) {
    case 4445: // enrollment page1
        $path = get_bloginfo('template_directory');
        $source = $path . "/js/ajax_test.js";
        wp_enqueue_script(
                $handle='this-is-my-ajax-handle',
                $source,
                $dependencies = array('jquery')
        );
        break;
    }


    switch ($page_num) {
    case 4445: // enrollment page1
        $data = array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('ajaxin_it'),
            'action' => 'ajaxin_it'
        );

        wp_localize_script('this-is-my-ajax-handle','ajaxYall', $data);     
        break;
    }

}
add_action('init', 'my_init');

// Define functions for ajax:

$my_action = 'ajaxin_it';
if (defined('DOING-AJAX') && DOING_AJAX) {
    // for loggedin users
    add_action('wp_ajax_' . $my_action, 'do_stuff_pp');
    // for non logged in users
    add_action('wp_ajax_nopriv_' . $my_action, 'do_stuff_pp');
}

function do_stuff_pp(){

    $response = array(
        'status' => 'error',
        'message' => "It's AJAX Y'all!"
    );

    // check is a legitimate call
    if(isset($_GET['nonce']) && wp_verify_nonce($_GET['nonce'], 'ajaxin_it')){

        $response = array(
            'status' => 'success',
            'message' => "It's AJAX Y'all!"
        );
    }
    header('Content: application/json');    
    echo json_encode($response);
    die();
}

This is the .js:

jQuery(document).ready(function($){  

    $('#text_ajax_yall').click(function(){
        var $el = $(this);

    alert(ajaxYall.nonce);
    $.ajax({url:ajaxYall.ajaxurl,action:ajaxYall.action,nonce:ajaxYall.nonce,type:'GET',dataType:'json',success:function(response, textStatus, jqXHR){
        if( 200 == jqXHR.status && 'success' == textStatus ) {
            if( 'success' == response.status ){
                        $el.after( '<p style="color:green;">' + response.message+ '</p>' );
                    } else {
                        $el.after( '<p style="color:red;">' + response.message + '</p>' );
            }
        }
//      $el.after( '<p style="color:green;">' + response.message + '</p>' );
        console.log(response.message, textStatus, jqXHR );      
    }});

    });

});

What is the specific cause of this and how can I fix it?

Upvotes: 0

Views: 1144

Answers (2)

Snake Eyes
Snake Eyes

Reputation: 16764

Buddy, you have no prova property defined in your PHP code.

You have something like:

$response = array(
        'status' => 'error',
        'message' => "It's AJAX Y'all!"
    );

So you have to call:

console.log(response.status);

or

 console.log(response.message);

Upvotes: 1

SergkeiM
SergkeiM

Reputation: 4168

response.prova You try to get the value but as I saw in php you dont have array with value prova... Try to console.log just response lik this

console.log(response);

In array you have 2 values 'status' and 'message'

so basically it will be response.status and response.message

Upvotes: 1

Related Questions